Profiel van MichaelMichael S. CollierFoto'sWeblogLijstenMeer Extra Help

Weblog


    25 september

    Using the Stopwatch class in .NET 2.0

    I recently came across the Stopwatch class while browsing through some code online.  I’ve never heard this class before, so I took a closer look.  I talked to a few friends to find out if they’ve heard of it – nope.  So, I figure this would be a good time to do some .NET evangelism.  :)  Can I get an ‘Amen’ from the congregation?

    Have you ever tried to determine how long some chunk of code takes to run?  If so, I'll bet you've used the DateTime and TimeSpan classes to do so.  You've probably written sometime like this:

       1: // Set the start time.
       2: DateTime startTime = DateTime.Now;
       3:  
       4: // Do some work.
       5: DoWork();
       6:  
       7: // Set the end time.
       8: DateTime stopTime = DateTime.Now;
       9:  
      10: // Determine the elapsed time.
      11: TimeSpan elapsedTime = stopTime - startTime;
      12:  
      13: // How long did the work take?
      14: Console.WriteLine(string.Format("It took {0} milliseconds to complete the work.", elapsedTime.Milliseconds));

    That's all well and good, I suppose.  But, there is another way.

    The Stopwatch class was introduced in .NET 2.0, and provides a clean way to measure elapsed time.  Take this code for example:

       1: Stopwatch stopwatch = new Stopwatch();
       2:  
       3: // Start the timer!
       4: stopwatch.Start();
       5:  
       6: // Do some work.
       7: DoWork();
       8:  
       9: // Stop the timer!
      10: stopwatch.Stop();
      11:  
      12: // How long did the work take?
      13: Console.WriteLine(string.Format("It took {0} milliseconds to complete the work.", stopwatch.ElapsedMilliseconds));
      14: Console.WriteLine(string.Format("It took {0} ticks to complete the work.", stopwatch.ElapsedTicks));
      15:  
      16: // Start the timer, again!
      17: stopwatch.Start();
      18:  
      19: // Do some more work.
      20: DoWork();
      21:  
      22: // Stop the timer, again!
      23: stopwatch.Stop();
      24:  
      25: // How long did the work take?
      26: Console.WriteLine(string.Format("It took {0} milliseconds to complete the work.", stopwatch.ElapsedMilliseconds));

    As you can see here, the Stopwatch class provides a simple Start() and Stop() method that can be used to measure elapsed time.  After stopping the stopwatch, you can start it up again and time continues.  It's just like, well, a real stopwatch.  If you want to stop the timer and reset the elapsed time, simply call the Reset() method.  Easy and clean!!

    Another feature of the Stopwatch class is that it automatically uses a high-resolution timer if the underlying hardware and OS support it.  Otherwise, the Stopwatch class uses the system timer (the DateTime class).

    To read more about the Stopwatch class, read the documentation on MSDN at http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(VS.80).aspx.

     
    *