I'm porting a library from Windows to *NIX (currently OSX), does anyone now what function can I use instead of Microsoft's QueryPerformanceCounter and QueryPerformanceFrequency?
5 Answers
On OSX mach_absolute_time
and mach_timebase_info
are the best equivalents to Win32 QueryPerformance* functions.
See http://developer.apple.com/library/mac/#qa/qa1398/_index.html

- 14,761
- 17
- 70
- 94
http://www.tin.org/bin/man.cgi?section=3&topic=clock_gettime (and the other functions mentioned there) - it's Posix! Will fall back to worse counters if HPET is not existent. (shouldn't be a problem though)
http://en.wikipedia.org/wiki/High_Precision_Event_Timer
Resolution should be about +10Mhz.

- 4,777
- 5
- 35
- 55
-
20`clock_gettime` is part of the POSIX Realtime Extension, and is unfortunately not implemented on Mac OS X. – Left For Archive May 20 '11 at 01:13
-
Not a great answer. clock_gettime isn't supported, and it returns time in seconds, rather than counter ticks (even if it was supported). – Justicle Feb 14 '14 at 19:25
Try boost's ptime for portable high-resolution timing.
Update (prompted, 2 years on, by Mark's comment below):
These days I'd use a std::chrono::high_resolution_clock
; example.

- 24,582
- 12
- 83
- 135
-
Looks like that will work on linux/mac, but quoting: Get the UTC time using a sub second resolution clock. On Unix systems this is implemented using GetTimeOfDay. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution. – Mark Harviston Apr 13 '12 at 13:48
-
Thanks, ptime not so portable as I thought. Still, you prompted me to update my answer here. See also http://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer – timday Apr 13 '12 at 17:58
He asked about OS X. Use these APIs from CoreAudio:
AudioConvertHostTimeToNanos(AudioGetCurrentHostTime())

- 29
- 1
Back in the day you had either uclock or you delved into assembler to read the RDTSC.
G.

- 16,230
- 17
- 74
- 137
-
If you are using a high-resolution timer, the chances are you aren't going to be going into powersaving mode. – graham.reeds Jan 23 '09 at 11:47
-
You may not be, but some other process may put the system into that mode. It depends on what kind of constraints your system has. For the general public its not guaranteed. – ApplePieIsGood Jan 23 '09 at 16:12
-
4It will also screw up if your process switches cpus, if I remember correctly ... we have done some evaluation using tsc + linux and the effect of powersaving features. Each core has its own tsc. – Ronny Brendel Jan 24 '09 at 19:07
-
2Recent AMD and Intel chips implement consistent RDTSC. Good enough for me. – Marius Jul 07 '10 at 15:14