Accurate Timing is something that seems simple until you actually try it, at first people would be like just get two timestamps and minus them, sure that works for per second timing but what about millisecond or even microsecond timing?
Just get 2 millisecond timestamps and minus them, right? No, the problem comes into that when the CPU is heavily loaded, often the timestamps will be significantly off, i noticed this in my surveillance project, where a specified time of 10seconds ended up being between 5 to 15 real seconds depending on the load on the processor.
So how do you do accurate high resolution timing in windows? Before people made use of the now antiquated Multimedia timers, these days you simple use the Windows API QueryPerformanceCounter and QueryPerformanceFrequency functions.
The QueryPerformanceCounter function call returns the value of the performance counter (in counts) at that point and the QueryPerformanceFrequency returns the amount of counts per second for that processor… You can see where i’m going with this, Simple huh?
The only other thing you need to take into account is that you need to make use of the LARGE_INTEGER union type for the count storage, since the count is a 64bit int you need to make use of the QuadPart (LongLong) of the union for storage.
here’s a very basic c++ timing class: HRTimer.h