.net - .NET 中的精确时间

标签 .net vb.net time timing

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

我需要微秒级精度。

有没有在 .NET 中执行此操作的简单方法?

最佳答案

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

  • 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.

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

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

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

当然,如果您要计时较短的持续时间,则此效果根本无关紧要。

关于.net - .NET 中的精确时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1562850/

相关文章:

mysql - Rails - 如何向模型添加时间戳?

c# - 简单的数据库处理

c# - (C#,连接四)- 为下面的按钮着色

c# - 源代码控制系统的算法?

html - 如何使用 html 来设置 vb.net 桌面应用程序的样式?

.net - 使用 PngBitmapEncoder 的 RenderTargetBitmap 在没有显示时在虚拟机上生成空白图像,Windows 10 版本 1903

javascript - 无论本地时区如何,都获得赫尔辛基本地时间

java - 用于插入自定义代码 Eclipse 的键绑定(bind)键

.net - DLLImport在哪里寻找非托管DLL?

java - .net 上的 java servlet 等价物是什么