MSDN's Johan Nilsson on timing
Turns out the Windows System Time (GetSystemTime, which is actually under the hood of the .NET DateTime class) only has an effective resolution of 10-15 milliseconds. Sure, the data returned by GetSystemTimeAsFileTime has units of 100 nanoseconds, but if you were to repeatedly call the function you would find that in practice it only updates after many milliseconds.
The solution? To use QueryPerformanceCounter to supplement the times returned by GetSystemTime(). But now we have to call two functions in series, so the scheduler could pre-empt us, there's interrupt overhead, etc. Dual processor systems can mitigate this problem by using SetThreadAffinityMask to control for those factors. Otherwise, this limitation will put something of a floor on maximum performance.
But there's a problem with that, too - QPC was designed to count clock cycles, not long periods of time. The oscillators in PCs have terrible drift; the author notes that the time reported by QPC differed from the System Time by one millisecond in less than two minutes. This is consistent with my own timing observations. The author handles this by compensating for errors in QueryPerformanceFrequency. One caveat that I should add is that the error in reported frequency will change with time. I once studied the drift and it changes with a period of 24 hours. I'm pretty sure it peaks during the day and troughs at night, but I may have that backwards.
It also turns out that QPC can jump forward unexpectedly under heavy PCI bus load. You also have to account for NTP potentially changing the System Time behind your back, though it (usually) sends a WM_TIMECHANGE message to all top-level windows when it does this.
Finally, the author mentions that in uniprocessor systems, QPC doesn't directly read the Pentium Time Stamp Counter (TSC), but in multi-processor systems it does. QPC therefore has "poor" performance in a uniprocessor system, and the author created an experimental version of the time_provider which directly reads the TSC which has much better performance.
Thoughts of a random geek
Friday, October 24, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment