Simple way to check your code performance

Hi guys,Of course, we all know foreach loop takes more time than for loop and there are lot of similar scenarios in .Net. Even if it takes lot of time, we'll be forced to use foreach loop at some cases. So it'll be better, if we come to know the time taken for executing a piece of code at the runtime. Here is a simple way to find out the time taken for each process.It just writes the start time and finish time taken for the process in the debug window. Debug.Indent() method simply changes the indentation of the Output by one level and Debug.WriteLine() method writes a string in the debug window.

Here is the snippet

//Increases the current IndentLevel

System.Diagnostics.Debug.Indent();

//Writes the starttime

System.Diagnostics.Debug.WriteLine("DEBUG START TIME -> : " + DateTime.Now.ToString("HH:mm s:fff"));

//Execute the code

ConfigureControls();

//Writes the finish time

System.Diagnostics.Debug.WriteLine("DEBUG FINISH TIME -> : " + DateTime.Now.ToString("HH:mm s:fff"));

//Reduces the current IndentLevel

System.Diagnostics.Debug.Unindent();

5 Comments

  • If you use System.Diagnostics.StopWatch you can more accurately measure elapsed time.

  • thanks man

  • I have to side with Linefeed...

    the System.Diagnostics.StopWatch class is much better at this.

  • Thanks guys. Stopwatch gives u precised results.

    Stopwatch sw = Stopwatch.StartNew();
    PerformWork();
    sw.Stop();

    Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

    N.B : Please visit link for more information

    http://stackoverflow.com/questions/28637/is-datetimenow-the-best-way-to-measure-a-functions-performance

  • Fairly portion of ctnneotI merely came across your website as well as within accession funds in order to declare that We obtain really loved accounts your own website postsAny method Ic3a2ll end up being signing up for your rss feeds as well as We satisfaction you receive correct associated with admittance in order to regularly quickly.

Comments have been disabled for this content.