posted on Tuesday, October 26, 2004 7:31 AM
by
Saravana
StopWatch Class in Whidbey
Just now came from gym, thought of posting about StopWatch Class which i have used today during my performance testing of generics(will post performance results later....) before i go to home.
StopWatch is a new class introduced in Whidbey used to accurately measure elapsed time. I was expecting this class from vb6 days...now it is available in framework itself. Previously if i want to measure any performance testing. Either i use DataTime.Now() or Enviorment.TickCount() to measure elapsed time between any operation. But using these methods, resolution can go maximum up to 500ms. But using StopWatch class, you can accurately measure elapsed time(you can measure up to nanoseconds) with few lines of code. For example,
Stopwatch sw = new Stopwatch();
List<double> list = new List<double>();
sw.Start();
for (int i = 0; i < count; i++)
{
list.Add(<double>);
}
long addTime = sw.ElapsedMilliseconds;
sw.Reset();
Double d = 0;
sw.Start();
for (int i = 0; i < count; i++)
{
for (int j = 0; j < 30; j++)
{
d = list[i];
}
}
long getTime = sw.ElapsedMilliseconds;
sw.Stop();
Start the stopwatch, measure elapsed time using ElaspsedMilliseconds() property. For more details about stopwatch class, check this beta msdn link (http://msdn2.microsoft.com/library/ebf7z0sw.aspx).