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