关于高分辨率性能计数器及其与.NET秒表相关的存在的解释?
在静态 Stopwatch 构造函数中,我们可以看到以下代码,它主要检查高分辨率性能计数器是否存在。
static Stopwatch()
{
if (!SafeNativeMethods.QueryPerformanceFrequency(out Frequency))
{
IsHighResolution = false;
Frequency = 0x989680L;
tickFrequency = 1.0;
}
else
{
IsHighResolution = true;
tickFrequency = 10000000.0;
tickFrequency /= (double) Frequency;
}
}
在 MSDN 上,它提到了QueryPerformanceFrequency
:
检索高分辨率性能计数器的频率(如果存在)
但是,它到底何时存在还很不清楚?我怀疑它通常存在于当前的机器上,但具体什么时候不存在呢?
这很有趣,因为当它不存在时,Stopwatch
就变成了 DateTime.UtcNow
属性的简单包装。
Inside the static Stopwatch
constructor we can see the following code, that basicly checks whether a high-resolution performance counter exists.
static Stopwatch()
{
if (!SafeNativeMethods.QueryPerformanceFrequency(out Frequency))
{
IsHighResolution = false;
Frequency = 0x989680L;
tickFrequency = 1.0;
}
else
{
IsHighResolution = true;
tickFrequency = 10000000.0;
tickFrequency /= (double) Frequency;
}
}
On MSDN it says about QueryPerformanceFrequency
:
Retrieves the frequency of the high-resolution performance counter, if one exists
It's pretty unclear, however, when exactly does it exist? I suspect it usually exists on current machines, but when exactly doesn't it?
It's interesting because when it doesn't exist, Stopwatch
becomes a mere wrapper around the DateTime.UtcNow
property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计时器和秒表之间存在差异,混淆两者会导致错误的假设。不幸的是,计时器一词经常被用来表示多种含义。
任何运行 Windows 2000 或更高版本的计算机都可能具有高频计时器。我从来没有遇到过运行 Windows 2000 以后版本的计算机没有这样的东西。
现在,这就是高频定时器。还有计时器:Windows 或.NET 组件。这些计时器不用于计时或测量时间,而是用于定期执行操作。 Windows 计时器对象的分辨率为 1 毫秒,当计算机不参与 CPU 密集型操作时非常可靠。 .NET 计时器对象的分辨率限制为大约 15 毫秒。您可以通过使用 P/Invoke 直接与 Windows 对象交互来解决这个问题,但这通常不是必需的。
.NET Stopwatch 类基于高频计时器。一般来说,
Start
查询性能计数器并存储该值。当您停止
时,它会再次查询性能计数器。经过的时间是这两个值的简单减法。您可以从秒表获得比微秒更好的分辨率。事实上,您可以使用带有忙等待循环的秒表,为您提供亚毫秒级的分辨率。我怀疑你能否用它获得亚微秒分辨率。
需要理解的重要一点是,尽管计时器的可靠性不超过 1 毫秒,但测量经过的时间的秒表要精确得多。您可能可以信任秒表的微秒级经过时间测量。除此之外,我不会指望它。
There is a difference between a timer and a stopwatch, and confusing the two leads to erroneous assumptions. Unfortunately, the term timer is used to mean multiple things all too often.
Any machine that runs Windows 2000 or later likely has a high frequency timer. I have never run across a computer that runs Windows 2000 later that does not have such a thing.
Now, that's the high frequency timer. There are also timers: Windows or .NET components. These timers are not used for keeping time or for measuring time, but rather for performing actions at periodic intervals. The Windows timer objects are capable of 1 ms resolution, and are very reliable when the computer is not involved in CPU intensive operations. The .NET timer objects are limited to approximately 15 ms resolution. You can get around that by using P/Invoke to interact directly with the Windows objects, but it's not often necessary.
The .NET Stopwatch class is based on the high frequency timer. In general,
Start
queries the performance counter and stores the value. When youStop
, it queries the performance counter again. The elapsed time is a simple subtraction of those two values. You can get better than microsecond resolution from the Stopwatch.And in fact, you can use a
Stopwatch
with a busy-waiting loop that gives you sub-millisecond resolution. I doubt that you could get sub-microsecond resolution with it.The important thing to understand is that, although timers aren't reliable beyond 1 millisecond, the stopwatch, which measures elapsed time, is much more precise. You can probably trust microsecond-level elapsed time measurements from Stopwatch. Beyond that, I wouldn't count on it.
系统时间的实际分辨率约为毫秒(尽管声称是 1/10 毫秒,但我从未见过能够可靠提供该分辨率的工作站)。
但是,您可以使用其他硬件(例如时间码读取器)来提供低至纳秒的分辨率。这些通常用于与IPC(工业PC)一致的工业控制系统中以同步DAQ硬件。您可以在 PCI Express 插槽卡。适用于 PCI Express 薄型 x1 本地总线的时间码处理器。
普通工作站几乎总是使用高分辨率计数器,但实际上,如果没有额外的硬件,分辨率将不会达到您所期望的(微秒或纳秒)。
Actual resolution of the system time is on the order of milliseconds (even though it is claimed to be 1/10th of a millisecond, I've never seen a workstation that provides that resolution reliably).
However, you can use additional hardware (like time code readers) which provides resolutions down to nanoseconds. These are usually used in industrial control systems in line with IPCs (industrial PCs) to synchronize DAQ hardware. You can see an example with 100 ns resolution in PCI Express Slot Cards. Time code processor for PCI express low-profile x1 local bus.
Ordinary workstations will almost always use high-resolution counters, but actually resolution will not be what you would expect (micro or nano seconds) without additional hardware.