13

This question is inspired by Does Linux provide a monotonically increasing clock to applications.

Maybe I should be more precise: I'm looking for a clock function which is strictly increasing, thus never returning the same value, independant how quick two calls follow each other.

Community
  • 1
  • 1
gyrolf
  • 3,772
  • 5
  • 26
  • 22

1 Answers1

8

Yes, GetTickCount() does this. If you want a higher fidelity counter, QueryPerformanceCounter is also available. Neither of these counters depend on the time of day.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    Should change your link to GetTickCount64() -- from your link, the return value of GTC wraps after about 50 days. – John Millikin Oct 17 '08 at 07:03
  • GetTickCount64() is only available on Windows Vista or Windows Server 2008. – Greg Hewgill Oct 17 '08 at 07:05
  • As long as you can rely on being called at least once a month or so, you can cobble together a GetTickCount64() wrapper around GetTickCount(). – Michael Burr Oct 17 '08 at 07:34
  • 1
    Be careful with QueryPerformanceCounter because it can leap forward unexpectedly by several seconds, so you have to compare it constantly with GetTickCount: [Microsoft KB274323](http://support.microsoft.com/kb/274323/en-gb). – Tyler Streeter Sep 24 '14 at 12:54
  • 3
    The above does not answer the (edited) original question since none of the suggestions provide a _strictly increasing_ clock. – Per Mildner Sep 15 '17 at 12:07
  • @TylerStreeter That KB isn't available anymore. MS std::chrono::steady_clock uses QPC timers – Pavel P Nov 26 '18 at 22:49