The Stopwatch Class in .NET

Do you ever find yourself using DateTime to time a section of code?  Do you have code like the following?

  <span style="color: rgb(128,0,128)">DateTime</span> start <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(128,0,128)">DateTime</span><span style="color: rgb(255,0,0)">.</span>Now;

  <span style="color: rgb(0,128,0)">// Perform a long process
</span>   <span style="color: rgb(43,145,175)">Thread</span><span style="color: rgb(255,0,0)">.</span>Sleep( <span style="color: rgb(128,0,128)">1968</span> );

  <span style="color: rgb(128,0,128)">DateTime</span> end <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(128,0,128)">DateTime</span><span style="color: rgb(255,0,0)">.</span>Now;
  <span style="color: rgb(128,0,128)">TimeSpan</span> duration <span style="color: rgb(255,0,0)">=</span> end<span style="color: rgb(255,0,0)">.</span>Subtract( start );
  <span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>WriteLine( <span style="color: rgb(163,21,21)">"This process took {0} ms"</span>, 
    duration<span style="color: rgb(255,0,0)">.</span>TotalMilliseconds );

If you do, you should look at the System.Diagnostics.Stopwatch class that was introduced in the 2.0 framework.  You can convert the code above to the much more readable

  <span style="color: rgb(43,145,175)">Stopwatch</span> stopwatch <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">Stopwatch</span>();
  stopwatch<span style="color: rgb(255,0,0)">.</span>Start();

  <span style="color: rgb(0,128,0)">// Perform a long process
</span>  <span style="color: rgb(43,145,175)">Thread</span><span style="color: rgb(255,0,0)">.</span>Sleep( <span style="color: rgb(128,0,128)">1968</span> );

  stopwatch<span style="color: rgb(255,0,0)">.</span>Stop();
  <span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>WriteLine( <span style="color: rgb(163,21,21)">"This process took {0} ms"</span>, 
    stopwatch<span style="color: rgb(255,0,0)">.</span>ElapsedMilliseconds );

3 thoughts on “The Stopwatch Class in .NET

  1. Thanks for the comment. I didn’t know about the static method.

    I do a similar thing by deriving from the Stopwatch and logging the elapsed time in the Dispose. I was going to blog about that next, but I guess you beat me to it ;)

  2. You can actually make this even shorter. There is a static method on the Stopwatch class called StartNew() that returns an instance that has been started for you.

    ex.

    Stopwatch myStopwatch = Stopwatch.StartNew();

    DoStuff()

    myStopwatch.Stop();
    Console.WriteLine(“Do Stuff took {0} ms”, myStopwatch.ElapsedMilliseconds);

    I’ve got a class called a StopwatchWriter that simplifies everything if you’re profiling multiple parts of the same method as well.

    http://www.vonsharp.net/StopwatchWriterClass.aspx

Comments are closed.