A Stopwatch Class for .NET 1.1

The first rule of performance testing is to measure, then measure again, then measure one more time just to be sure. NET 2.0 adds a handy Diagnostics.Stopwatch which is perfect for this kind of ad-hoc precision timing.

A year ago I created a Stopwatch class which was eerily similar to the one that Microsoft ended up shipping in .NET 2.0. I went ahead and made the minor modifications necessary to make my Stopwatch class identical to Microsoft's. This way you can use the object in .NET 1.1 and port your code as-is to .NET 2.0 with only a namespace change.

using System;
/// <summary>
/// Provides a set of methods and properties that you can use to accurately
/// measure elapsed time.
/// </summary>
public class Stopwatch
{
[System.Runtime.InteropServices.DllImport("Kernel32")]
static extern bool QueryPerformanceCounter(out long @ref);
[System.Runtime.InteropServices.DllImport("Kernel32")]
static extern bool QueryPerformanceFrequency(out long @ref);
private long _Start = 0;
private long _Elapsed = 0;
private bool _IsRunning = false;
/// <summary>
/// the current performance-counter frequency, in counts per second
/// </summary>
readonly private long _CounterFrequency;
public Stopwatch()
{
/// prelinks all win32 api calls so there's less performance hit when called
System.Runtime.InteropServices.Marshal.PrelinkAll(typeof(Stopwatch));
if (QueryPerformanceFrequency(out _CounterFrequency) == false)
{
throw new Exception("High resolution timers are not available on this CPU");
}
}
/// <summary>
/// Starts, or resumes, measuring elapsed time for an interval.
/// </summary>
public void Start()
{
if (_IsRunning) this.Stop();
_Start = this.CurrentTime;
_IsRunning = true;
}
/// <summary>
/// Stops measuring elapsed time for an interval.
/// </summary>
public void Stop()
{
if (!_IsRunning) return;
_Elapsed += this.CurrentTime - _Start;
_Start = 0;
_IsRunning = false;
}
/// <summary>
/// Stops time interval measurement and resets elapsed time span to zero.
/// </summary>
public void Reset()
{
if (_IsRunning) this.Stop();
_Elapsed = 0;
}
/// <summary>
/// retrieves the current value of the high-resolution performance counter.
/// </summary>
private long CurrentTime
{
get
{
long l = 0;
QueryPerformanceCounter(out l);
return l;
}
}
/// <summary>
/// Indicates whether the Stopwatch timer is running.
/// </summary>
public bool IsRunning
{
get { return (_IsRunning); }
}
/// <summary>
/// Gets the total elapsed time measured by the current instance.
/// </summary>
public TimeSpan Elapsed
{
get { return new TimeSpan(this.ElapsedTicks); }
}
/// <summary>
/// Gets the total elapsed time measured by the current instance, in milliseconds
/// </summary>
public long ElapsedMilliseconds
{
get
{
if (_Elapsed == 0) return 0;
return (long)(((double)_Elapsed / _CounterFrequency) * 1000);
}
}
/// <summary>
/// Gets the total elapsed time measured by the current instance, in timer ticks
/// </summary>
public long ElapsedTicks
{
get
{
return (long)(this.ElapsedMilliseconds * TimeSpan.TicksPerMillisecond);
}
}
}

Did you ever wonder how the QueryPerformance* Win32 API functions work their magic and provide accurate near-nanosecond timing results? There's some interesting trivia about these functions on Matt Walsh's wiki.

Related posts

Why Ruby?

I've been a Microsoft developer for decades now. I weaned myself on various flavors of home computer Microsoft Basic, and I got my first paid programming gigs in Microsoft FoxPro, Microsoft Access, and Microsoft Visual Basic. I have seen the future of programming, my friends, and it is

By Jeff Atwood ·
Comments

Donating $5,000 to .NET Open Source

Way back in June of last year, I promised to donate a portion of my advertising revenue back to the community: I will be donating a significant percentage of my ad revenue back to the programming community. The programming community is the reason I started this blog in the first

By Jeff Atwood ·
Comments

Do Not Buy This Book

A few friends and I just wrote a book together: The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks. I met K. Scott Allen, Jon Galloway, and Phil Haack through their excellent blogs. That online friendship carried over into real life. We always thought it'd

By Jeff Atwood ·
Comments

Defining Open Source

As I mentioned two weeks ago, my plan is to contribute $10,000 to the .NET open source ecosystem. $5,000 from me, and a matching donation of $5,000 from Microsoft. There's only two ground rules so far: 1. The project must be written in .NET managed

By Jeff Atwood ·
Comments

Recent Posts

Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
The Great Filter Comes For Us All

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven’t any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don’t stop there – read the Story of Your Life novella it was based on for so much

By Jeff Atwood ·
Comments
I Fight For The Users

I Fight For The Users

If you haven’t been able to keep up with my blistering pace of one blog post per year, I don’t blame you. There’s a lot going on right now. It’s a busy time. But let’s pause and take a moment to celebrate that Elon Musk

By Jeff Atwood ·
Comments
The 2030 Self-Driving Car Bet

The 2030 Self-Driving Car Bet

It’s my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger use

By Jeff Atwood ·
Comments