This question has been asked before (see What's the equivalent of Windows' QueryPerformanceCounter on OSX?), but it seems like my needs are a bit different than in other questions, so I'm going to ask again.
All I'm looking for is a tick counter - I don't need UTC or anything like that. And I need a timer that isn't affected by sleep-mode - I need a tick counter that accurately reflects how many ticks have passed, even while the machine was in sleep mode.
QueryPerformanceCounter()
on win32 has a number of nice properties that fit my requirements:
- High resolution, down to microseconds
- Continues counting, even in system sleep (or rather its count includes time that went by when the system was in sleep mode)
- Monotonic - never goes down. Not affected by changing the system time.
On OSX, as far as I can tell, I have the following alternatives, and none of them are very good:
gettimeofday()
- resolution is down to milliseconds (ok, not great)
- continues counting in system sleep (good)
- is affected by changing the system clock (bad)
mach_absolute_time()
- resolution is down to microseconds (good)
- doesn't count in system sleep (bad)
- not affected by changing system clock (good)
host_get_clock_service( SYSTEM_CLOCK )
&clock_get_time()
(see mach/clock.h)- seems to be identical to
mach_absolute_time()
- seems to be identical to
host_get_clock_service( CALENDAR_CLOCK )
&clock_get_time()
(see mach/clock.h)- seems to be identical to
gettimeofday()
- seems to be identical to
I don't think that I fully appreciated just how great QueryPerformanceCounter()
was until I started looking at this problem. How does it work? Does it use the HPET? Does the HPET go to sleep when the system goes to sleep? If not, why doesn't OSX expose the HPET this way? If so, how does Windows compensate for this?
Update 1:
There's an interesting discussion of how QueryPerformanceCounter
works here: http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx
Apparently it once used CPU TSCs in Win XP days, switched to using the HPET/ACPI timers in Vista, and switched again to improved TSCs in Win 7 but falling back to HPET/ACPI. Still, I was always under the impression that the HPET and TSC didn't survive sleep mode.