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);

Posted in Software | 8 Comments

8 Responses to Units of mach_absolute_time()

  1. Jesse Towner says:

    Thanks for the information.

  2. Nathan says:

    Thanks! No telling how many hours you just saved me.

  3. Justin says:

    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 says:

      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.

  4. Julia says:

    Great information! Thanks!

    And don’t forget to:

    #include

  5. Julia says:

    mach/mach_time.h

  6. Justin says:

    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 :-)

  7. orion says:

    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.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>