Tuesday 26 July 2011

Measure Execution Time taken by your code.

To measure an execution time taken by your code, just use the below piece of code at appropriate place in your code block.
using System.Diagnostics;

public static void Main(string[] args)
{
Console.WriteLine("In Main");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

//Your Code will go here

stopwatch.Stop();
var timeSpan = stopwatch.Elapsed;
var executionTimeTaken=String.Format("{0:00}:{1:00}:{2:00}.{3:00}",timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
Console.WriteLine(executionTimeTaken);
}

Thanks,
Md. jawed