1. Technology

How do I do High Resolution Timing in C# on Windows?

A Very Accurate Timer

By

How do I time my C# Code?

The .NET Framework 2.0 (but not the Compact .NET Framework) includes a class called Stopwatch. This uses the high resolution timer that we have already used in the C and C++ How Do's, unless it's not available. I haven't come across a case yet where it isn't available. I suspect it depends on older CPUs which may not have the counter. There is a read only property of the Stopwatch class called IsHighResolution which returns true if the readings use the high resolution counter.

How To do the Timing

Using this is very easy. Just declare an instance of the class then call Start() to start timing, Stop() to stop and read the Elapsed property, which has public properties such as days, hours, minutes, seconds and milliseconds that can be displayed.

Download Example 1 - ex1.cs

The code below is the main part of example 1 and shows these methods being called.

    Stopwatch st=new Stopwatch();
    st.Start() ;
    Console.WriteLine("Started") ;
    for (ulong i2 = 0; i2 < 10000000; i2++)
    {       ulong ul2 = i2;
      ul2++;
    }     st.Stop() ;
    Console.WriteLine("Elapsed = {0}", st.Elapsed.ToString()) ;
    if (Stopwatch.IsHighResolution)     {
      Console.WriteLine("Timed with Hi res") ;
    }     else
      Console.WriteLine("Not Timed with Hi res") ;

If this was timed with the high resolution counter then it will be accurate to microseconds. It is actually accurate to nanoseconds (10-9 seconds, ie a billionth of a second) but there is so much other stuff going on that nanosecond accuracy is really a bit pointless. When doing timing or benchmarking of code, you should do a number of runs and take the average time- because of other processes running under Windows, how much swapping to disk is occurring etc, the values between two runs may vary. A good tip is to reboot before the timings. That way there are fewer programs running and RAM is less likely to be fragmented.

On .NET Before Version 2.0

If you haven't yet moved to .NET 2.0, you can still do timings with another timing class that I have written and this is also called Stopwatch in the hires.cs file.

This implements a similar type class as the .NET 2.0 Stopwatch class though it has fewer members and elapsed returns the time in seconds as a double.

Download hires.cs

This uses a static variable and function getFrequency() so that the private field frequency is only initialized once, irrespective of the number of instances of this class that are created. This works like the C and C++ "How Do Is" and uses the Win32 API calls to QueryPerformanceCounter() and QueryPerformanceFrequency() functions. Just call the QueryPerformanceCounter() function twice, and the returned number of ticks is divided by the frequency to give the elapsed time in seconds.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. How To Do Things in C#
  6. How do I do High Resolution Timing in C# on Windows?

©2014 About.com. All rights reserved.