39

Does anyone know if the HiPerfTimer or the StopWatch class is better for benchmarking, and why?

1
  • 4
    Its dated 2002, when .NET 1.1 was around. +1
    – nawfal
    Apr 22, 2013 at 12:58

3 Answers 3

43

Stopwatch is based on High resolution timer (where available), you can check that with IsHighResolution

22

They are the same when it comes to high resolution timing.

Both use this:

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long PerformanceCount);

and this:

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long Frequency);

to do the underlying timing. (You can verify this with Reflector.NET). I'd use StopWatch because it's part of the framework already (no need to link another dll) and it had better features than HiPerfTimer.

2
  • 6
    Yes- however the StopWatch is actually superior because it has a fallback option. The HiPerfTimer throws an Win32Exception if a high performance counter is not supported.
    – RichardOD
    Sep 28, 2009 at 7:46
  • 1
    Yes I eventually gave up on HiPerfTimer. The article on CodeProject is a little flimsy (but the idea was good). I also recommended StopWatch, it's more robust and will has a default when QueryPerformanceFrequency fails, which is linked to the IsHighResolution mentioned by Shay. Sep 28, 2009 at 7:50
7

StopWatch- it also works on systems that don't support a high resolution performance counter and you don't need any external libraries to use it.

The other one throws an Win32Exception if there is no support for a high resolution counter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.