How accurate is System.Diagnostics.Stopwatch?
Asked Answered
A

9

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.

Anthropolatry answered 26/12, 2008 at 17:27 Comment(6)
Is millisecond accuracy enough? Should be sufficient for your purposes. Define "accurate".Pule
i know it can give you milliseconds but i have been told that there are many cases where its not accurateAnthropolatry
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.Physostomous
Thanks for the question. I didn't even know there was such a class in the framework until now. Heh.Christos
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.aspxMyna
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.Nereidanereids
E
0

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

There are some good open source profilers, like:

Emelun answered 26/12, 2008 at 18:27 Comment(5)
Note that the NProf site at Google Code says NProf is no longer under development -- it recommends SlimTune instead.Footnote
Useful information, but this is an answer for a different question.Jaunty
This does not answer the question, as stated.Advocation
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 (?)Blooded
@Philm: Agreed, but it's unfortunate that this is the top Google result for "C# stopwatch resolution"Multipara
M
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

Myna answered 4/3, 2010 at 12:30 Comment(2)
Seconds is a lower case s. ms not mS. en.wikipedia.org/wiki/Second#SI_multiples (same in your article)Foin
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)?Blooded
N
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

Nominalism answered 17/11, 2015 at 21:55 Comment(0)
S
2

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

Speedometer answered 26/12, 2008 at 17:30 Comment(3)
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!Bertrand
Precision and Accuracy are not the same thing. Your computer does not have nanosecond accuracy. It doesn't even have millisecond accuracy.Classroom
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.Foin
D
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.

Diatessaron answered 26/12, 2008 at 18:3 Comment(1)
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.Myna
D
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

Downhill answered 18/3, 2010 at 10:19 Comment(0)
R
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!

Rossner answered 26/12, 2008 at 18:20 Comment(0)
E
0

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

There are some good open source profilers, like:

Emelun answered 26/12, 2008 at 18:27 Comment(5)
Note that the NProf site at Google Code says NProf is no longer under development -- it recommends SlimTune instead.Footnote
Useful information, but this is an answer for a different question.Jaunty
This does not answer the question, as stated.Advocation
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 (?)Blooded
@Philm: Agreed, but it's unfortunate that this is the top Google result for "C# stopwatch resolution"Multipara
B
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.

Botha answered 28/11, 2011 at 14:26 Comment(1)
Looking at the source code for Stopwatch class (using .NET Reflector), it's already a very thin wrapper around QueryPerformanceCounter.Time
R
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.

Remunerate answered 11/1, 2024 at 15:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.