How accurate is System.Diagnostics.Stopwatch?

后端 未结 8 2084
花落未央
花落未央 2020-11-30 10:55

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 stopwatc

相关标签:
8条回答
  • 2020-11-30 11:42

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

    There are some good Open Source profilers like:

    • NProf
    • Prof-It for C#
    • NProfiler
    • ProfileSharp
    0 讨论(0)
  • 2020-11-30 11:43

    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.

    0 讨论(0)
提交回复
热议问题