Using HPET for a high-resolution timer on Windows

Unfortunately, despite the promise of a new regime in 2005, it’s still not automatic; there’s work for you to do.

Even though most motherboards have the HPET timer now, it seems to be disabled by default. There’s an easy way to see if this is true or false – QueryPerformanceCounter will return a value in the 14 million range if HPET is enabled (it’s a 14 MHz timer), and something in the 3 million range if HPET is disabled (the older chip timer).

Now, this is new behavior – QueryPerformanceCounter, some years ago, returned the TSC counter, which is very high-resolution, but has huge swings with power saving modes, and as processors increased in power, power savings turns on all the time. So, Microsoft, with little fanfare, switched QueryPerformanceCounter back to using timers on motherboards. So, if you’re running an older Microsoft OS, you might get a value in the 100 million range if you call QueryPerformanceCounter, and then the following doesn’t apply to you. The bridge was somewhere in the Vista time range, but I’ve seen Vista systems that use TSC for QPC, as well as HPET/RTC for QPC.

void test_time()
{
    LARGE_INTEGER frequency;
    if (!::QueryPerformanceFrequency(&frequency))
    {
        fprintf(stderr, "failed, err=%d\n", ::GetLastError());
        exit(1);
    }
    fprintf(stdout, "freq = %lld\n", frequency.QuadPart);
}

With HPET disabled, I get freq = 3262656 as the output, or 3.26 Mhz. With HPET enabled, I get freq = 14318180 as the output, or 14.3 Mhz. This is on a Windows 7 machine with an Intel i7 975 processor and chipset. The HPET clock listed above will measure intervals with a precision of 70 nanoseconds; while this won’t help time very small sequences of instructions, this will be reasonably precise at the microsecond range.

If your BIOS has HPET enabled, then you can enable HPET in Windows with a bcdedit command, and disable it with a different bcdedit command.

Enable use of HPET

bcdedit /set useplatformclock true

Disable use of HPET

bcdedit /deletevalue useplatformclock

You’ll need to reboot to see changes, because this is a boot-time option (hence the use of bcdedit to change it).

Enabling HPET will change the performance of your system; people tend to inadvertently tune their programs to the specific behavior of a clock. It would be nice if people didn’t do that, but it happens. Anecdotal information says “makes things smoother but slower”, and this would match the idea of applications tuned to a slower clock.

Reference

Bad performance? Try enabling HPET (Win 7) makes the claim that this improved frame rate for some and decreased it for others.

The High Precision Event Timer page on Wikipedia is a good reference.

 

One thought on “Using HPET for a high-resolution timer on Windows”

  1. “freq = 3262656″ is *NOT* “the older chip timer”. You do not know what you are talking about.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>