Archive for February, 2009

February
7th 2009
A Debug Stopwatch (In C#)

Posted under Open Source & Performance

Time is of the utmost importance to almost any performance test, but, in .net, it can require a bit of scaffolding code. A triplet of C# classes can make short work of measuring performance, and of collating and persisting the results, in a handy XML format.

In the wild, this is often done with code similar to the following:

   Stopwatch sw = new Stopwatch();

   sw.Start();
   // Do some work here
   sw.Stop();

   Debug.WriteLine(sw.Elapsed.TotalMilliseconds.ToString();

This is hardly the end of the world, but it’s a bit cumbersome, especially over the course of many tests.  Perhaps a more natural construction would be:

   using (DebugStopwatch countIndexOf = new DebugStopwatch("IndexOf")) {
      // Do some work here
   }

… with the results being harvested later.  The literal text “IndexOf” can be replaced with any label that will be meaningful when you analyze the timing data.  Persistence of test results is accomplished through the magic of static classes. Continue Reading »

No Comments »