Units of mach_absolute_time()
As programmers on OS X are well aware, the units of mach_absolute_time()
have been nanoseconds since 10.2, although this was only documented since 10.5. As the iPhone runs a slimmed down version of OS X, I thought it reasonable that the units would remain the same on the device.
[Update] As Justin points out below, this was never the case. A complete misread of the documentation on my part. I managed to work on only machines for which the units of mach_absolute_time()
were nanoseconds.
So if you're trying to get some timing from the iPhone using mach_absolute_time()
and seeing times that indicate faster results on the iPhone, (not that this happened to me...), make sure to account for the difference in timebase.
Here's the code for conversion between the mach_absolute_time()
units and nanoseconds:
#include <mach/mach_time.h> /* Get the timebase info */ mach_timebase_info_data_t info; mach_timebase_info(&info); uint64_t start = mach_absolute_time(); /* Do some code */ uint64_t duration = mach_absolute_time() - start; /* Convert to nanoseconds */ duration *= info.numer; duration /= info.denom; /* Log the time */ NSLog(@"My amazing code took %lld nanoseconds!", duration);
9 comments:
Jesse Towner at 2008-10-11 17:51:50 -0400
Thanks for the information.
Nathan at 2008-10-18 21:41:00 -0400
Thanks! No telling how many hours you just saved me.
Justin at 2009-02-28 06:03:10 -0500
I believe you should always use the values in mach_timebase_info, because they could be different from system to system depending on the host CPU and bus, even just on Mac OS X.
Devin Lane at 2009-03-26 19:39:47 -0400
A good suggestion. While the values are documented to be 1:1 on Mac OS X on all systems up until now, and this will likely continue into the future, code that needs more portability may benefit from using mach_timebase_info as you suggest.
Julia at 2009-04-12 02:19:47 -0400
Great information! Thanks! And don't forget to: #include
Julia at 2009-04-12 02:20:51 -0400
mach/mach_time.h
Justin at 2009-04-19 06:59:55 -0400
I just checked on my G4 PowerBook running 10.5 - its exactly 30 nanoseconds per tick. So its never been guaranteed to be 1:1. Win32 has a similar function "QueryPerformanceFrequency()". Even different Intel/AMD systems have different frequencies, depending on BIOS settings of the various motherboard manufactures. More information here: http://msdn.microsoft.com/en-us/library/bb173458.aspx So unless your code is guaranteed to be only running on your own personal mac, please use mach_timebase_info() - that's what its for :-)
orion at 2011-04-23 22:50:01 -0400
great post, thank you. i was noticing that something i was animating based on CACurrentMediaTime was subtly jerky and staccato, so i replaced CACurrentMediaTime with a wrapper around mach_absolute_time and it's much better. this is iPad + raw openGL. my guess is that altho CACurrentMediaTime is a double, it actually only has millisecond accuracy, which then bumps into aliasing on the 60Hz render timer.
Peter Krehl at 2016-12-30 23:29:00 -0500
Recently, I came across the term "mach absolute time unit." I have three questions: 1) What is the definition of this term? 2) Who coined this term? 3) Does "mach" refer to the Austrian philosopher Ernst Mach? Contrary to Sir Newton, Mach was a strict opponent of an absolute time (even prior to A. Einstein) because of relativity reasons. Therefore, for physicists this term is somewhat misleading. Thank you in advance for any comments.