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.

Read more

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

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

By Jeff Atwood · · Comments

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

By Jeff Atwood · · Comments

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

By Jeff Atwood · · Comments