秒表与使用 System.DateTime.Now 计时事件

发布于 2024-09-02 21:21:08 字数 259 浏览 10 评论 0原文

我想跟踪代码的性能,因此我使用 System.DateTime.Now 存储开始和结束时间。我将两者之间的差异作为我的代码执行时间。

但我注意到这种差异似乎并不准确。所以我尝试使用秒表对象。事实证明,这要准确得多。

谁能告诉我为什么Stopwatch比使用System.DateTime.Now计算开始时间和结束时间之间的差异更准确?

顺便说一句,我说的不是十分之一。我得到了大约 15-20% 的差异。

I wanted to track the performance of my code so I stored the start and end time using System.DateTime.Now. I took the difference between the two as the time my code to execute.

I noticed though that the difference didn't appear to be accurate. So I tried using a Stopwatch object. This turned out to be much, much more accurate.

Can anyone tell me why Stopwatch would be more accurate than calculating the difference between a start and end time using System.DateTime.Now?

BTW, I'm not talking about a tenths of a percent. I get about a 15-20% difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一笑 2024-09-09 21:21:18

计时函数性能链接讨论了您的确切问题,特别是本段:

问题是,根据 MSDN,DateTime.Now 函数的分辨率是 10+ 毫秒,我们需要调用它两次!因此,这会在运行时间测量中引入 20+ 毫秒的波动。由于我们的函数调用通常可能比这个 20 毫秒窗口返回得快得多,因此这还不够好。

编辑:这听起来像是第二个 DateTime.Now 在与秒表方法结束时不同的时间被调用。

this timing function performance link discusses your exact problem, specifically this paragraph:

The problem is that according to MSDN the resolution of the DateTime.Now function is 10+ milliseconds, and we need to call it twice! So that would introduce a 20+ ms swing in your runtime measurement. As our function calls are often likely to be a lot quicker to return than this 20ms window this isn’t good enough.

EDIT: It kind of sounds like the second DateTime.Now is being called at a different time than when the stopwatch method concludes.

玩套路吗 2024-09-09 21:21:17

最好使用 Stopwatch 类,因为它比减去 DateTime 值要准确得多:

Stopwatch s = Stopwatch.StartNew();
// Tested code here
s.Stop();
Console.WriteLine("Elapsed Time: {0} ms", s.ElapsedMilliseconds);

不幸的是,这段简单的代码在大多数情况下不足以获得准确的测量结果,因为在该类的底层有很多活动在进行。操作系统,它会窃取一些 CPU 时间并减慢代码的执行速度。这就是为什么最好多次执行测试,然后删除最低和最高时间。对于该 puprose,有一个方法可以多次执行测试代码,删除最低和最大时间并计算平均时间,这是一个很好的解决方案。我在我的博客中发布了示例测试方法

It's better to use the Stopwatch class because it's much more accurate than subtracting DateTime values:

Stopwatch s = Stopwatch.StartNew();
// Tested code here
s.Stop();
Console.WriteLine("Elapsed Time: {0} ms", s.ElapsedMilliseconds);

Unfortunately, this simple piece of code won't be enough to get accurate measurements most of the times because there’s a lot of activity going on under the hood of the OS, which can steal some CPU time and slow down the execution of your code. That’s why it is best to execute the tests multiple times and then remove the lowest and the highest times. For that puprose it is a good solution to have a method that executes the tested code multiple times, removes the lowest and the greatest times and calculates the average time. I have published a sample testing method in my blog.

无人接听 2024-09-09 21:21:15

根据 MSDN

秒表通过计算底层计时器机制中的计时器滴答数来测量经过的时间。如果安装的硬件和操作系统支持高分辨率性能计数器,则 Stopwatch 类将使用该计数器来测量经过的时间。否则,Stopwatch 类使用系统计时器来测量经过的时间。使用Frequency 和IsHighResolution 字段确定秒表计时实现的精度和分辨率。

它使用比 DateTime.Now 更高的分辨率/精度。

您还可以查看以下相关链接:

Environment.TickCount 与 DateTime.Now

DateTime.Now 是衡量函数性能的最佳方法?

DateTime 足以精确到秒,但超出此范围我会推荐 StopWatch

As per MSDN:

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

It uses a higher resolution / precision than DateTime.Now.

You can also check out these related links:

Environment.TickCount vs DateTime.Now

Is DateTime.Now the best way to measure a function's performance?

DateTime is good enough for precision to the second probably but anything beyond that I would recommend StopWatch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文