1

There are lots of questions (here, here, here) about mechanisms for getting monotonic time on Windows and their various gotchas and pitfalls. I'm particularly interested in the accuracy (not precision) of the main options.

I'm looking to measure elapsed time on a single machine, when the time is on the order of multiple minutes to an hour. What i know so far:

  • QueryPerformanceCounter is great for short time intervals, but QPF can have error on the order of 500PPM, which translates to error of 2 seconds over an hour.
    • More concerning is that even on fairly recent processors, folks are seeing QPC misbehavior.
    • Microsoft recommends QPC above all else for short-term duration measurements. But short-term isn't defined in any absolute numbers.
  • GetTickCount64 is often cited as a nice and reliable, less precise alternative for QPC.
    • I've not found any good details about the accuracy of GetTickCount64. While it is less precise than QPC, how does its accuracy compare? What kind of error might I expect over an hour?
    • Some programs play with its resolution by using timeBeginPeriod, although I don't think this affects accuracy?
    • The docs talk about how GetTickCount64's resolution is not affected by adjustments made by the GetSystemTimeAdjustment function. Hopefully this means GetTickCount64 is monotonic and not adjusted ever? It is unusual wording...
  • GetSystemTimePreciseAsFileTime is an option for same-machine time deltas if I disable automatic time adjustment via SetSystemTimeAdjustment. It is backed by QPC. Is there any benefit to using this over QPC directly? (Perhaps it does sanitization or thread affinity tricks to avoid some of the issues encountered by direct QPC calls?)
aggieNick02
  • 2,334
  • 2
  • 21
  • 31
  • I think the biggest piece I am missing to answer my question is what serves as the time source for GetTickCount64? I don't see it called out anywhere. The docs talk about how the system provides an interrupt based on the default period or whatever is specified via timeBeginPeriod, but not what time source is used to provide that interrupt. – aggieNick02 May 15 '18 at 16:26

1 Answers1

1

One SO QA I found linked to this blog post, which has been particularly useful to read. While it doesn't answer my question directly, it dives into how QPC works on Windows, and how the common linux monotonic time basically uses the same thing.

The gist is that both of them use rtdsc when an invariant TSC on modern hardware is available.

aggieNick02
  • 2,334
  • 2
  • 21
  • 31