2

To get accurate time measurements on iOS, mach_absolute_time() should be used. Or CACurrentMediaTime(), which is based on mach_absolute_time(). This is documented in this Apple Q&A, and also explained in several StackOverflow answers (e.g. https://stackoverflow.com/a/17986909, https://stackoverflow.com/a/30363702).

When does the value returned by mach_absolute_time() wrap around? When does the value returned by CACurrentMediaTime() wrap around? Does this happen in any realistic timespan? The return value of mach_absolute_time() is of type uint64, but I'm unsure about how this maps to a real timespan.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Stephan Palmer
  • 835
  • 7
  • 18

1 Answers1

2

The document you reference notes that mach_absolute_time is CPU dependent, so we can't say how much time must elapse before it wraps. On the simulator, mach_absolute_time is nanoseconds, so if it's wrapping at UInt64.max, that translates to 585 years. On my iPhone 7+, it's 24,000,000 mac_absolute_time per second, which translates to 24 thousand years. Bottom line, the theoretical maximum amount of time captured by mach_absolute_time will vary based upon CPU, but you won't ever encounter this in any practical application.

For what it's worth, consistent with those various posts you found, the CFAbsoluteTimeGetCurrent documentation warns that:

Repeated calls to this function do not guarantee monotonically increasing results. The system time may decrease due to synchronization with external time references or due to an explicit user change of the clock.

So, you definitely don't want to use NSDate/Date or CFAbsoluteTimeGetCurrent if you want accurate elapsed times. Neither ensures monotonically increasing values.

In short, when I need that sort of behavior, I generally use CACurrentMediaTime, because it enjoy the benefits of mach_absolute_time, but it converts it to seconds for me, which makes it very simple to use. And neither it nor mach_absolute_time are going to loop in any realistic time period.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Great, thank you, that helped me out a lot! I got consistent values for several other iDevices (over 24000 years on iPad Mini 1, iPad Air 2, iPad Pro First Generation, iPhone 6), and the Simulator as well (nearly 585 years). I used the `mach_timebase_info` function, as described in the previously mentioned [Apple Q&A](https://developer.apple.com/library/content/qa/qa1398/_index.html). – Stephan Palmer Aug 09 '17 at 08:57