Partilhar via


timerHelper: Provides high-precision timing

The first utility class I intend to describe is timerHelper. The purpose of this class is to provide a high-precision timing function. Please note that it is not a timer that issues events, instead is used to measure how long something has taken to execute. The purpose of providing an additional timer (there are several available in .NET Framework) is that this implementation provides more accurate timings.

 

I use it to time frequently called or long-running methods in my code to gain an understanding of how long these tasks take. With the new code profiling features of Visual Studio 2005 you can get this information during system testing, however using the timerHelper class you can get the timings even in a production environment which may help you understand where your bottlenecks are in a live system. Also, I sometimes think that the VS profiling provides you with too much information to drill down into (which also is a good thing).

 

Another reason I prefer this class over VS profiling is that I can use it in any type of application; windows app, windows service, web, BizTalk, MIIS, etc. Most of my projects involve BizTalk and there is no profiling support for BizTalk.

 

The implementation is based on a utility class got sent to me on a mailing alias in September 2001. I took that class and made a few simplifications, e.g. removing some statistical functions. Over the years the class has only had minor changes made to it, and just recently I made it even simpler to use by starting the timer in the constructor (previously you had to call Start).

 

Typically the class is used like this:

timerHelper timer = new timerHelper();

// Execute some code here which should be timed!

Trace.WriteLine("methodname: Duration: " + timer.StopDuration() + " ms");

 

There some more "advanced" scenarios where you stop and get the duration multiple times, thereby getting partial and complete timings. Each call to Stop will only timestamp the end time, the start time remains the same, so by not restarting the timer you can get "lap times" while still getting the full "race" time.

 

The actual implementation of timerHelper is an encapsulation of the Win32 APIs QueryPerformanceCounter and QueryPerformanceFrequency.

 

You can find the implementation file as an attachment. Download it and give it a go!

 

An alternative implementation can be found at https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto09.asp. I guess we have started from the same file back in 2001 as the basic design is identical to my class. However, this class uses nanoseconds instead of milliseconds so if you need extreme precision this might be an option. For me milliseconds are just fine as I mostly time things that are in the millisecond range (if not even seconds).

 

[Update]:

As Richard noted in his comment .NET 2.0 provides us with the System.Diagnostics.Stopwatch class, which does essentially the same thing.

 

Read more here: https://msdn2.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

timerHelper.cs

Comments