Running time of an application in Visual Studio-Collection of common programming errors


  • Shakehar

    I am solving some algorithm problems in c# and running them as a console application.

    To check for the efficiency of the applications I would like to see what their run times are.

    Currently I am printing the time at the start of a program and at the end and calculating the time difference ,but is there a way to reduce Observer’ effect ?
    Some inbuilt tool/plugin that I am not aware of ?


  • SLaks

    You should use the Stopwatch class, which is specifically designed to do that.

    To avoid measuring the JIT time, you should also run each algorithm at least once before measuring anything so that the JIT has time to run.

    When measuring the algorithms, you should run each one hundreds of times and take the average runtime.


  • user694833

    The most important source of delay due to observer’s effect is printing itself. Another potential delayer is debug message text formatting. So I suggest the following:

    • If you can anticipate the number of loops and number of stages per loop, create an array to store timing information. If not, use a dynamic list.
    • During execution, store time and aditional information in that array or list.
    • If possible, don’t store messages along with time but codes, for example 1=Stage 1, 2=Stage 2, etc.
    • At the end of the execution, dump all the information to screen or file, and format messages as needed.