Wednesday, June 22, 2011

How to use Stopwatch in C#


Create an object of Stopwatch class and start it, then execute the method, logic whose execution time is to be calculated. Next stop the watch shown below.

Stopwatch watch = new Stopwatch();
watch.Start();           

for (int i = 0; i < 100000; i++)
{
   for (int j = 0; j < 20000; j++)
   { 
                    // To waste some time....:P
   }
}
watch.Stop();
double time_wasted = watch.Elapsed.TotalSeconds;
// To round 
double time_wasted_rounded = Math.Round(watch.Elapsed.TotalSeconds);

0 comments:

Post a Comment

 

2011 ·Code-Studio by yrus.