How accurate is System.Diagnostics.Stopwatch?

leora picture leora · Dec 26, 2008 · Viewed 29.2k times · Source

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.

Answer

Thomas Maierhofer picture Thomas Maierhofer · Mar 4, 2010

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

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