none
General Questions About C# High Performance RRS feed

  • Question

  • Hello,

    When you make a performance you can use the syntax code Stopwatch() to make a measurement but I have some general question about it.

    1.
    Should you use the syntax code "Stopwatch stopwatch = new Stopwatch();" in production phase. I have heard that this syntax takes a lot performance.
    Is there another approach to use it?

    2.
    Is there any other option / tool that is not to use "Stopwatch stopwatch = new Stopwatch();" in order to make a mesurement?

    3.
    Is there any other advice that I should be aware about high performance?


    Thank you!
    Thursday, January 23, 2020 1:03 PM

All replies

  • Hello,

    You should check out using a profiler at runtime rather than use one or more StopWatches. Start with Profiling overview.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 23, 2020 1:09 PM
    Moderator
  • Like Karen noted profiling is tool for measuring performance.

    What you also need to do is decide what kind of an things you measure. Is it just execution time or maybe also resource usage? Memory? Disk? CPU? GPU? Then what is the baseline for profiling and also what is the impact of profiling (since it usually has some)? What kind of an environment you use for profiling? And things like that.

    Thursday, January 23, 2020 2:35 PM
  • Hi Sakura Data,

    Thank you for posting here.

    As Kareninstructor suggested, profiler is a good choice to profile your .NET Framework application.

    What Are Some Good .NET Profilers?

    >>Is there any other advice that I should be aware about high performance?

    Here are two related references you can refer to.

    1. .NET Performance Tips
    2. C# Performance tips and tricks

    Hope them could be helpful.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 28, 2020 8:25 AM
    Moderator
  • "1.Should you use the syntax code "Stopwatch stopwatch = new Stopwatch();" in production phase. I have heard that this syntax takes a lot performance.
    Is there another approach to use it?"

    That depends on exactly what you are testing and how your test is setup. But in general you do not need to create (and delete) a new stopwatch instance over and over you can just use the same one. Declare the object at class level (global) on app startup and then reset/restart the same class level stopwatch for each loop.

    When you create an instance the system must execute the code in the constructor and reserve the memory reqd for the object. That takes time and memory. Using a class level stopwatch just requires starting and stopping the same stopwatch.

     

    "2.Is there any other option / tool that is not to use "Stopwatch stopwatch = new Stopwatch();" in order to make a mesurement?"

    With a Stopwatch use  MyStopwatch.Reset etc.

    Another option is to simply subtract the system clock time elapsed during the test. ie :

        elapsed time = finshtime - starttime.

    Furthermore, one can use threading or async methods to make timing loops for testing.



    Tuesday, January 28, 2020 1:51 PM