Runtime Debugger

kannan M ambadi

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();

Comments

Linefeed said:

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

# March 25, 2008 5:27 AM

kannan.ambadi said:

thanks man

# March 25, 2008 8:11 AM

Eric Newton said:

I have to side with Linefeed...

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

# March 26, 2008 11:55 AM

kannan.ambadi said:

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

stackoverflow.com/.../is-datetimenow-the-best-way-to-measure-a-functions-performance

# October 8, 2008 7:33 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)