Reputation: 363
Is it possible to get Time stamps with use of Query performance counter (Win32,C++) ? If not what is the most accurate way of obtaining time stamps on Win32-C++ application?
Upvotes: 1
Views: 1843
Reputation: 6220
What clock sources do you have in hardware?
You cannot use QueryPerformaceCounter if you want accuracy, since it ticks with the CPU clock, which is fast but not accurate.
The interrupt timer: This timer generates an interrupt with a frequency of 1 kHz and is probably quite exact.
The HPET: A 1 MHz timer. This one is the one you want to use.
The audio interface sample clock: The frequency of this clock depends on audio sample rate, but the interrupt frequency depends on audio buffer size, which typically is requiered to be longer than some ms.
I have successfully used a Waitable Timer to drive a screen capturing program at intervals of 40 ms. For a demo, see: http://www.youtube.com/watch?v=SLad8-IRtg4 . The audio track were recorded by an analog feedback from the audio out to audio in, demonstrating that the frame clock keep in sync with the sample clock of the audio device. The same clock should work in 1 ms, but not when the machine is busy rendering 3d stuff and compressing video frames to PNG :-)
To use audio device clock, record to a dummy buffer. On windows 7, use the new audio architecture to get the smallest possible buffer size. On older systems, you need kernel streaming. If you go for an audio sultion, you can also plug a function generator into your wave device and have a loop that returns a timestamp based on trigg condition. This way you will go below 1 ms.
Upvotes: 0
Reputation: 4925
QueryPerformanceCounter
is just a counter that contains some value when the machine is powered on and counts up. It's not connected to the wall clock at all.
GetSystemTime
and GetSystemTimeAsFileTime
are accurate to ~15ms if that's good enough.
If you're targeting Windows 8 only then GetSystemTimePreciseAsFileTime
is very precise.
If you need a really high resolution time on Windows pre 8 you can try a hybrid approach using the system time and the performance counter something like what is outlined here: Implement a Continuously Updating, High-Resolution Time Provider for Windows
If you're using C++ 11, then you might want to look at std::chrono::system_clock
or std::chrono::high_resolution_clock
as well.
Upvotes: 3