September 21st, 2017

How accurate are the various Windows time-querying functions?

Windows has a bunch of time-querying functions. One group of functions uses the system performance counter. These are as accurate as the hardware allows while still conforming to the basic requirements (such as running at a constant speed and being consistent among multiple processors).

  • Query­Performance­Counter gives you the current performance counter, and
  • Query­Performance­Frequency tells you the frequency of the performance counter.

Another group uses the system timer, which usually means 55ms or 10ms, although the time­Begin­Period function can be used to run the timer at a higher rate.

  • Get­Tick­Count, Get­Tick­Count64
  • Get­Message­Time
  • Get­System­Time, Get­Local­Time, Get­System­Time­As­File­Time
  • Query­Interrupt­Time, Query­Unbiased­Interrupt­Time

And then there are the so-called precise¹ variants of the system timer functions. These take the system timer value and combine it with the system performance counter to get a high-accuracy timestamp. It’s not only querying two timers, but it’s also doing additional computations to combine the values, so naturally this is slower than querying just one of the two timers, but hey, a fine wine takes time.

  • Get­System­Time­Precise­As­File­Time
  • Query­Interrupt­Time­Precise, Query­Unbiased­Interrupt­Time­Precise

These high-accuracy functions give you the best of both worlds: Time correlated with real-world clocks, but with the accuracy of the system performance counter. But as noted, it comes at a performance cost.

¹ Though to be pedantic, they should be called the accurate variants.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.