32

How accurate is System.Diagnostics.Stopwatch? I am trying to do some metrics for different code paths and I need it to be exact. Should I be using stopwatch or is there another solution that is more accurate?

I have been told that sometimes stopwatch gives incorrect information.

6
  • 1
    Is millisecond accuracy enough? Should be sufficient for your purposes. Define "accurate".
    – duffymo
    Dec 26, 2008 at 17:29
  • i know it can give you milliseconds but i have been told that there are many cases where its not accurate
    – leora
    Dec 26, 2008 at 17:31
  • Don't ever rely on a single measurement of anything for accuracy. Run each code path for several minutes and count the number of iterations to get an average.
    – HUAGHAGUAH
    Dec 26, 2008 at 17:33
  • 6
    I've written an article about that. It shows how you get good measurements out of the stopwatch class: codeproject.com/KB/testing/stopwatch-measure-precise.aspx Mar 4, 2010 at 12:33
  • 3
    BTW, since this is a very old question now, in newer .NETs running on newer hardware, stopwatch accuracy is much better, because (as Lee Grisholm says in a comment on Kernelman's answer), it is now a wrapper around QueryPerformanceCounter. Sep 23, 2015 at 9:42

9 Answers 9

22

I've just written an article that explains how a test setup must be done to get an high accuracy (better than 0.1 ms) out of the stopwatch. I think it should explain everything.

Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch

2
  • 2
    Seconds is a lower case s. ms not mS. en.wikipedia.org/wiki/Second#SI_multiples (same in your article)
    – Jannes
    Feb 10, 2012 at 22:47
  • Your article somewhat contradicts to what MS states here: msdn.microsoft.com/en-us/library/windows/desktop/… MSDN. They claim that for most hardware, Stopwatch() is fastest and accurate especially for multiprocessor CPUs and frequence-changing-CPUs (which are most modern CPUs I would guess). Do you still think, it is worth, to run time-manamgent on only one thread (if possible)?
    – Philm
    May 3, 2017 at 20:31
7

The System.Diagnostics.Stopwatch class does accurately measure time elapsed, but the way that the ElapsedTicks method works has led some people to the conclusion that it is not accurate, when they really just have a logic error in their code.

The reason that some developers think that the Stopwatch is not accurate is that the ElapsedTicks from the Stopwatch DO NOT EQUATE to the Ticks in a DateTime. The problem arises when the application code uses the ElapsedTicks to create a new DateTime.

var watch = new Stopwatch();
watch.Start();
... (perform a set of operations)
watch.Stop();
var wrongDate = new DateTime(watch.ElapsedTicks); // This is the WRONG value.

If necessary, the stopwatch duration can be converted to a DateTime in the following way:

// This converts stopwatch ticks into DateTime ticks.
// First convert to TimeSpan, then convert to DateTime
var rightDate = new DateTime(watch.Elapsed.Ticks); 

Here is an article that explains the problem in more detail: http://geekswithblogs.net/BlackRabbitCoder/archive/2012/01/12/c.net-little-pitfalls-stopwatch-ticks-are-not-timespan-ticks.aspx

Note that the content is no longer available at the original link. Here is a reference to the archived content from the Wayback Machine: https://web.archive.org/web/20190104073827/http://geekswithblogs.net:80/BlackRabbitCoder/archive/2012/01/12/c.net-little-pitfalls-stopwatch-ticks-are-not-timespan-ticks.aspx

2

First, exact is of course not a possible or meaningful concept when talking about time or space, since no empyrical measurement of a physical magnitude can ever pretend to be exact.

Second, David Bolton's blog article may be useful. I'm quoting:

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.

1
  • 2
    The main problem is not disk I/O it is in fact thread context switches. With the newer versions of the .NET framework and Windows this problem has become less important, but it is still there. Sep 15, 2014 at 9:31
2

MSDN has some examples of the stopwatch. They also have it showing how accurate it is within nanoseconds.

3
  • 1
    I ran their sample code. It said it would be accurate to within 0 nanoseconds, and gave me a result of -657ms for one of the tests. Not very helpful! Dec 26, 2008 at 17:43
  • 2
    Precision and Accuracy are not the same thing. Your computer does not have nanosecond accuracy. It doesn't even have millisecond accuracy.
    – mmcdole
    Dec 26, 2008 at 18:45
  • 1
    Yes it does. Not always, but recent computers use the RDTSC CPU instruction. It can be accurate up to the frequency of the CPU (like 3 GHz). However, I'm having issues with some newer hardware where the StopWatch.Frequency is around 2MHz (so I'm guessing Windows is using some other clock on the motherboard), but for some reason that frequency is not very stable, leading to error into the milliseconds... which is why I'm browsing stackoverflow for it.
    – Jannes
    Feb 10, 2012 at 22:46
2

The Stopwatch class returns different values under different configurations as the frequency depends on the installed hardware and operating system.

Using the stopwatch class, we can have only the rough estimation of execution time. And for each execution, it returns a different value, so we have to take average of different execution.

More information: Stopwatch Class

0

Why don't you profile your code instead of focusing on microbenchmarks?

There are some good open source profilers, like:

5
  • 2
    Note that the NProf site at Google Code says NProf is no longer under development -- it recommends SlimTune instead. Apr 5, 2011 at 5:11
  • 11
    Useful information, but this is an answer for a different question.
    – A.R.
    Apr 15, 2015 at 21:12
  • 8
    This does not answer the question, as stated.
    – Adam White
    Dec 10, 2016 at 14:13
  • 1
    Answer to comments as "..does not answer the question" : On the contrary. This answers the question, if you read it: "Should I be using stopwatch or is there another solution that is more accurate". It only doesn't answer the headline of the question. Besides that, there are people here (as me) who see improvement suggestions as important and welcome answers, and for me this includes also references to alternative solutions. I have some problems to understand, why there seem to be not few people around here, who need to critisize that (?)
    – Philm
    May 3, 2017 at 10:33
  • @Philm: Agreed, but it's unfortunate that this is the top Google result for "C# stopwatch resolution" May 17, 2019 at 20:14
0

If you want more precise timings. Take a look at the QueryPerformanceCounter. MSDN link for QueryPerformanceCounter. A neat implementation is given here. The example loads coredll.dll for CE, for Windows you should load the Kernel32.dll as stated in the MSDN documentation.

1
  • 9
    Looking at the source code for Stopwatch class (using .NET Reflector), it's already a very thin wrapper around QueryPerformanceCounter. Jan 24, 2012 at 20:23
0

I realize this is a very old thread, but it's still the first hit and I'd like to add a bit that led me here in the first place. If you:

Console.WriteLine(timer.Elapsed.Seconds.ToString())

... you will periodically get very odd results, like "6" when it actually took much longer. That's because the correct call is the rather non-obvious-at-first-glance:

Console.WriteLine(timer.Elapsed.TotalSeconds.ToString())

... which of course makes sense in retrospect.

0

In addition to seconding the advice of HUAGHAGUAH, I'd add that you should be very skeptical of microbenchmarks in general. While close-focused performance testing has a legitimate place, it's very easy to tweak an unimportant detail. So write and verify code that is designed for readability and clarity, then profile it to find out where the hot spots are (or whether there are any worth worrying about), and then tune (only) those portions.

I recall working with a programmer who micro-optimized a bit of code that executed while the system waited for human input. The time savings absolutely disappeared in the between-keystroke lag!

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.