views:

1434

answers:

8
+3  Q: 

stopwatch accuracy

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.

A: 

Is millisecond accuracy enough? Should be sufficient for your purposes. Define "accurate".

duffymo
i know it can give you milliseconds but i have been told that there are many cases where its not accurate
ooo
Stopwatch can be very accurate if you have a correct test setup. Please read my article about that.http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
Thomas Maierhofer
+1  A: 

MSDN has some examples of the stopwatch. They also have it showing how accurate it is within Nanoseconds. Hope this helps!

Jab
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!
RedFilter
Precision and Accuracy are not the same thing. Your computer does not have nanosecond accuracy. It doesn't even have millisecond accuracy.
Simucal
+3  A: 

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
+2  A: 

First, exact is of course not a possible or meaningful concept when talking about time or space.

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.

Daniel Daranas
A: 

In addition to seconding the advice of HUAGHAGUAH above, I'd add that you should be VERY skeptical of micro-benchmarks 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!

joel.neely
+3  A: 

Why you don't profile your code instead of focusing on micro-benchmarks?

There are some good Open Source profilers like:

CMS
+1  A: 

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.

http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx

Thomas Maierhofer
A: 

Stopwatch class return different values under different configuration as Frequency depends on the installed hardware & operating system.

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

More Info : http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Aditya Acharya