SDL Timing in Windows

I was looking thru the source for SDL and didn’t see any references to the
QueryPerformanceCounter API for the Windows timing code; instead SDL seems
to be using GetTickCount. I have always thought that
QueryPerformanceCounter is the best API for Windows timing and have always
used that for my game timing. I would only fall back to GetTickCount if
QPC/high-res timer wasn’t available on the machine for some reason. Is
there any particular reason that SDL doesn’t use QPC? The only things I
can think of are:

-QPC is overkill; GetTickCount is just as good for our purposes.
-QPC may not work on all machines (although I am led to believe that this
would be very rare).
-I just didn’t see QPC in the source files I looked at (mebbe I shoulda
grep’ed the sources).

Kelly

I was looking thru the source for SDL and didn’t see any references to the
QueryPerformanceCounter API for the Windows timing code; instead SDL seems
to be using GetTickCount. I have always thought that
QueryPerformanceCounter is the best API for Windows timing and have always
used that for my game timing. I would only fall back to GetTickCount if
QPC/high-res timer wasn’t available on the machine for some reason. Is
there any particular reason that SDL doesn’t use QPC? The only things I
can think of are:

-QPC is overkill; GetTickCount is just as good for our purposes.
-QPC may not work on all machines (although I am led to believe that this
would be very rare).

Both of these are true. :slight_smile:
GetTickCount() provides millisecond precision timing, and QPC isn’t on
all systems (or you have to do some teaking, I forget which.)
If you need sub-millisecond precision, by all means, use QPC.

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

“slouken” == Sam Lantinga writes:

slouken> Both of these are true. :slight_smile: GetTickCount() provides
slouken> millisecond precision timing, and QPC isn’t on all systems
slouken> (or you have to do some teaking, I forget which.) If you
slouken> need sub-millisecond precision, by all means, use QPC.

Although GetTickCount looks like it returns millisecond precision,
it’s actually much less accurate than QueryPerformanceCounter,
returning values only accurate to 55ms on Win95/98 and 10ms on WinNT.
QueryPerformanceCounter’s precision is much better, varying between
1024Hz and the clock speed of your CPU depending on your OS and
hardware.

Whether or not the time returned by QueryPerformanceCounter slowly
drifts relative to realtime varies between different versions of the
OS. The ones that use rdtsc drift over time, as the CPU clock isn’t
synced with the CMOS clock. Those versions of Windows that base their
QueryPerformanceCounter on the interrupt driving the realtime clock
stay synced to it forever (as you’d expect) barring clock adjustments.

It is a big win to use QueryPerformanceCounter when it’s available,
especially for timing short intervals.

We determined most of the above information experimentally.

-Mat