How to realise long-term high-resolution timing on windows using C++?

心已入冬 提交于 2019-12-18 05:02:58

问题


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?

Thank you.


回答1:


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




回答2:


Which language are you using?

  • In Java (1.5 or above) I'd suggest 'System.nanoTime()' which requires no import.

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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/7583074/how-to-realise-long-term-high-resolution-timing-on-windows-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!