C# System.Diagnostics.Stopwatch 的准确度如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/394020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How accurate is System.Diagnostics.Stopwatch?
提问by leora
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.
System.Diagnostics.Stopwatch 的准确度如何?我正在尝试为不同的代码路径做一些指标,我需要它是准确的。我应该使用秒表还是有其他更准确的解决方案。
I have been told that sometimes stopwatch gives incorrect information.
有人告诉我,有时秒表会提供不正确的信息。
采纳答案by CMS
Why you don't profile your code instead of focusing on micro-benchmarks?
为什么你不分析你的代码而不是专注于微基准?
There are some good Open Source profilers like:
有一些不错的开源分析器,例如:
回答by Jab
MSDN has some examples of the stopwatch. They also have it showing how accurate it is within Nanoseconds. Hope this helps!
回答by Daniel Daranas
First, exactis 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:
其次,David Bolton的博客文章可能有用。我引用:
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.
如果这是用高分辨率计数器计时的,那么它将精确到微秒。它实际上精确到纳秒(10-9 秒,即十亿分之一秒),但是还有很多其他的事情发生,纳秒精度确实有点毫无意义。在对代码进行计时或基准测试时,您应该进行多次运行并取平均时间——因为在 Windows 下运行的其他进程、发生了多少磁盘交换等,两次运行之间的值可能会有所不同。
回答by joel.neely
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 areany worth worrying about), and then tune (only) those portions.
除了支持上述 HUAGHAGUAH 的建议之外,我还要补充一点,您应该对总体上的微基准非常怀疑。虽然密切关注的性能测试具有合法地位,但很容易调整不重要的细节。所以写并验证设计的可读性和清晰度码,然后点击个人资料就找出的热点(或是否有任何值得担忧的),然后调(只)的部分。
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!
我记得与一位程序员一起工作,他对系统等待人工输入时执行的一些代码进行了微优化。节省的时间在击键之间的延迟中完全消失了!
回答by Thomas Maierhofer
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.
我刚刚写了一篇文章,解释了必须如何进行测试设置才能从秒表中获得高精度(优于 0.1 毫秒)。我认为它应该解释一切。
http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
回答by Aditya Acharya
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
更多信息:http: //msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
回答by kernelman
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.
如果你想要更精确的时间。查看 QueryPerformanceCounter。QueryPerformanceCounter 的MSDN 链接。这里给出了一个简洁的实现。该示例为 CE 加载 coredll.dll,对于 Windows,您应该按照 MSDN 文档中的说明加载 Kernel32.dll。
回答by user3308241
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.
System.Diagnostics.Stopwatch 类确实可以准确地测量经过的时间,但是 ElapsedTicks 方法的工作方式让一些人得出结论,认为它不准确,因为他们的代码中确实只是存在逻辑错误。
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.
一些开发人员认为秒表不准确的原因是,秒表中的 ElapsedTicks 不等于 DateTime 中的 Ticks。当应用程序代码使用 ElapsedTicks 创建新的 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:
如有必要,可以通过以下方式将秒表持续时间转换为 DateTime:
// 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
这里有一篇文章更详细地解释了这个问题:http: //geekswithblogs.net/BlackRabbitCoder/archive/2012/01/12/c.net-little-pitfalls-stopwatch-ticks-are-not-timespan-ticks。 aspx