.NET 中的精确时间

发布于 2024-08-07 20:53:22 字数 84 浏览 3 评论 0 原文

我需要在 .NET 应用程序中访问非常精确的计时。

我需要微秒精度。

在 .NET 中是否有一种简单的方法可以做到这一点?

I need access to very precise timing in a .NET application.

I need microsecond precision.

Is there an easy way to do this in .NET?

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

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

发布评论

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

评论(6

阳光①夏 2024-08-14 20:53:22

System.Diagnostics.Stopwatch 是适合此目的的类计时的粒度,但请确保使用 Stopwatch 对象的 Elapsed.Ticks 属性而不是其 ElapsedTicks 属性,因为它们是两个不同的东西。

这可能是 .Net 中最微妙的潜在错误之一。

另外,请注意,秒表是高度精细的,但它不一定准确(取决于您对该术语的定义)。秒表返回的经过时间往往会偏离系统时间。

在我的笔记本电脑上,秒表在 24 小时内提前运行了近 5 秒,而这种效果在其他一些机器上甚至更糟。

当然,如果您计时的持续时间较短,则这种效果根本不重要。

System.Diagnostics.Stopwatch is the right class for this degree of granularity in your timing, but make sure you use your Stopwatch object's Elapsed.Ticks property instead of its ElapsedTicks property, as they are two different things.

  • ElapsedTicks (without the dot) refers to the number of ticks of the Stopwatch, and thus needs to be used in concert with Stopwatch.Frequency (which is not the same on all machines) to calculate the elapsed time.
  • Elapsed.Ticks (with the dot) gives the number of Timespan ticks, and is not dependent on the Stopwatch frequency.

This is probably one of the subtlest potential errors in .Net.

Also, beware that Stopwatch is highly granular, but it isn't necessarily all that accurate (depending on your definition of the term). The elapsed time returned by Stopwatch will tend to drift away from the system time.

On my laptop, Stopwatch runs almost 5 seconds ahead over 24 hours, and this effect is even worse on some other machines.

If you're timing short durations, of course, this effect should not matter much at all.

魂ガ小子 2024-08-14 20:53:22

秒表 类是您可以获得的最佳精度,它使用高频定时器。
秒表的频率取决于CPU的频率,但通常在每秒数百万次范围内。

如果CPU不提供高频定时器,那么该类将回退到每秒100-50次的系统定时器。

The Stopwatch class is the best precision that you can get, it uses the high frequency timer.
The frequency of the stopwatch depends upon the frequency of the CPU, but its usually in the millions times per second range.

If the CPU doesn't provide high frequency timer, then the class will fallback to system timer which is in the range 100-50 times per second.

小巷里的女流氓 2024-08-14 20:53:22

使用 Stopwatch 类,应该可以解决问题。

Use the Stopwatch class, should do the trick.

疾风者 2024-08-14 20:53:22

我发现了一种方法,它比 StopWatch、PerformanceCounters 和我遇到的所有其他实现稍微更精确,并且不需要 Platform Invoke 或其他方式。

请参阅https://stackoverflow.com/questions/15725711/obtaining-microsecond - precision-using-net-without-platform-invoke

如果您发现其他情况,请告诉我!

I have found a method which works slightly more precisely then StopWatch, PerformanceCounters and every other implementation I come across and does not require Platform Invoke or otherwise.

See https://stackoverflow.com/questions/15725711/obtaining-microsecond-precision-using-net-without-platform-invoke

Let me know if you find otherwise!

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