65.9K
CodeProject is changing. Read more.
Home

How To: Measure execution time in C#

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Mar 9, 2011

CPOL
viewsIcon

9820

Please try this method to get execution time. I'm not sure that it is very accurate.public static TimeSpan GetDuration(Stopwatch sw, ThreadStart method){ sw.Reset(); sw.Start(); method(); sw.Stop(); return sw.Elapsed;}you can put it into any common static class and use as...

Please try this method to get execution time. I'm not sure that it is very accurate.
public static TimeSpan GetDuration(Stopwatch sw, ThreadStart method)
{
	sw.Reset();
	sw.Start();
	method();
	sw.Stop();
	return sw.Elapsed;
}
you can put it into any common static class and use as follows:
IList dataObjects = GetObjects(); // execution time should be tested

foreach (DataObject do in dataObject)
{
}
Execution time can be measured in such way:
IList dataObjects = null;
Stopwatch sw = new Stopwatch();
Console.WriteLine("GetObjects executed in {0}", GetDuration(sw, delegate() {

dataObjects = GetObjects();

}));
foreach (DataObject do in dataObject)