StopWatch class in .Net 2.0 and above

.Net 2.0's StopWatch Class in the System.Diagnostics assembly gives you an efficient way of measuring time elapses for a single interval and also total of elapsed times across multiple intervals.

.Net 2.0's StopWatch Class in the System.Diagnostics assembly gives you an efficient way of measuring time elapses for a single interval and also total of elapsed times across multiple intervals.

 

This is a replacement for the old code that we used to write to measure the time elapses,

 

 

The good feature about this class is that it intelligently uses a resolution based on whether the hardware that it is run on supports a high one or not. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class measures elapsed time by counting timer ticks in the underlying timer mechanism. Otherwise, the Stopwatch class uses the system timer to measure elapsed time.

 e.g.

Stopwatch myWatch = new Stopwatch();

myWatch.Start();

// Some operation

myWatch.Stop();

int elapsedTime = myWatch.ElapsedMilliseconds;

Please Note: Do not forget to use the Reset method to clear the elapsed time after each use. Otherwise it will return the cumulative elapsed time value. However, you can create multiple instances also.

By [)ia6l0 iii   Popularity  (792 Views)