如何使用C++在windows上实现长期高分辨率计时?
我需要在很长一段时间(几个小时)内每几毫秒(20、30、40 毫秒)获取准确的时间戳。获取时间戳的函数由第三方库作为回调调用。
使用 GetSystemTime() 可以获得正确的系统时间戳,但只能达到毫秒精度,这对我来说不够精确。使用 QueryPerformanceTimer() 会产生更准确的时间戳,但在很长一段时间内与系统时间戳不同步(请参阅http://msdn.microsoft.com/en-us/magazine/cc163996.aspx)。
上面链接的站点提供的解决方案以某种方式仅适用于较旧的计算机,当我尝试在较新的计算机上使用它时,它会在同步时挂起。
在我看来,Boost 也只适用于毫秒精度。 如果可能的话,我想避免使用外部库,但如果没有其他选择,我会选择它。
有什么建议吗?
I need to get exact timestamps every couple of ms (20, 30, 40ms) over a long period of time (a couple of hours). The function in which the timestamp is taken is invoked as a callback by a 3rd-party library.
Using GetSystemTime()
one can get the correct system timestamp but only with milliseconds accuracy, which is not precise enough for me. Using QueryPerformanceTimer()
yields more accurate timestamps but is not synchronous to the system timestamp over a long period of time (see http://msdn.microsoft.com/en-us/magazine/cc163996.aspx).
The solution provided at the site linked above somehow works only on older computers, it hangs while synchronizing when i try to use it with newer computers.
It seems to me like boost is also only working on milliseconds accuracy.
If possible, I'd like to avoid using external libraries, but if there's no other choice I'll go with it.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 CodeProject 删除文章,这似乎是副本: DateTimePrecise C# 类 这个想法是使用
QueryPerformanceCounter
API 进行精确的小增量并定期进行调整它以保持长期准确性。这将提供微秒级的精度(“大约”是因为它仍然不完全精确,但仍然相当有用)。另请参阅:Windows 上的微秒分辨率时间戳
Deleted article from CodeProject, this seems to be the copy: DateTimePrecise C# Class The idea is to use
QueryPerformanceCounter
API for accurate small increments and periodically adjust it in order to keep long term accuracy. This is about to give microsecond accuracy ("about" because it's still not exactly precise, but still quite usable).See also: Microsecond resolution timestamps on Windows
您使用哪种语言?
请记住,在 Windows 中,时间片粒度为 1000ms / 64 = 15.625ms。
这将影响进程间通信,特别是在单处理器计算机上,或“同时”*运行多个 CPU 使用率高的进程的计算机上。
事实上,我刚刚通过 eBay 获得了 DOS 6.22 和 Windows for Workgroups 3.11/3.15,因此我可以截取我开始接触它时单处理器 Windows 机器的原始时间片配置。 (尽管在 3.0 以上的版本中可能看不到)。
Which language are you using?
Remember in Windows that time-slice granularity is 1000ms / 64 = 15.625ms.
This will affect inter-process communication, especially on uni-processor machines, or machines that run several heavy CPU usage processes 'concurrently'*.
In fact, I just got DOS 6.22 and Windows for Workgroups 3.11/3.15 via eBay, so I can screenshot the original timeslice configuration for uni-processor Windows machines of the era when I started to get into it. (Although it might not be visible in versions above 3.0).
在 Windows 上,您很难找到比 QueryPerformanceTimer() 更好的东西了。
在现代硬件上,它使用 HPET 作为源来取代 RTC 中断控制器。我希望 QueryPerformanceTimer() 和系统时钟是同步的。
You'll be hard pressed to find anything better than QueryPerformanceTimer() on Windows.
On modern hardware it uses the HPET as a source which replaces the RTC interrupt controller. I would expect QueryPerformanceTimer() and the System clock to be synchronous.
Windows 上没有这样的 QueryPerformanceTimer()。该资源名为
QueryPerformanceCounter()
。它提供以较高频率计数的计数器值。可以通过调用 QueryPerformanceFrequency() 来检索其递增频率。
由于该频率通常在 MHz 范围内,因此可以观察到微秒分辨率。
有一些实现,即这个线程或在Windows 时间戳项目
There is no such QueryPerformanceTimer() on windows. The resource is named
QueryPerformanceCounter()
. It provides a counter value counting at some higher frequency.Its incrementing frequency can be retrieved by a call to
QueryPerformanceFrequency()
.Since this frequency is typically in the MHz range, microsecond resolution can be observed.
There are some implementations around, i.e. this thread or at the Windows Timestamp Project