0

I'm trying to replace some deprecated functions in Xcode with newer ones although I can't quite seem to figure out how to make it work properly:

AbsoluteTime startTime = UpTime();
float frameTime = UnsignedWideToUInt64(AbsoluteDeltaToNanoseconds(UpTime(),startTime))/1E9;

I get two warnings with this statement:

'UpTime' is deprecated: first deprecated in macOS 10.8
'AbsoluteDeltaToNanoseconds' is deprecated: first deprecated in macOS 10.8

I looked at various other threads here, and tried to use:

NSTimeInterval systemUptime = [[NSProcessInfo processInfo] systemUptime] / 1E9;

It didn't seem to return the same result though:

1: 0.011983 // deprecated function
2: 0.000431 // replacement function
1: 0.007218
2: 0.000431
1: 0.007084
2: 0.000431

I presume that there's something else I need to do to get the correct value for the new function?

ctfd
  • 338
  • 3
  • 14
  • 1
    Seem like there is already solution described [here](https://stackoverflow.com/a/16072575/7063478). – nowaqq Nov 07 '17 at 23:13
  • @nowaqq: I looked at that answer before (that's where I got the newer function), but I'm unsure how to translate it properly into my "frameTime" variable - thanks. – ctfd Nov 07 '17 at 23:25
  • [Here](https://stackoverflow.com/a/4753909/7063478) you have getUptimeInMilliseconds implementation which replace AbsoluteToNanoseconds. You can do `uint64_t startTime = getUptimeInMilliseconds();` and then `float frameTime = ((uint64_t)getUptimeInMilliseconds() - startTime)/1E9;`. – nowaqq Nov 07 '17 at 23:38
  • @nowaqq: Unfortunately that did not work, as the result = `0.0000` – ctfd Nov 08 '17 at 00:31

1 Answers1

1

[[NSProcessInfo processInfo] systemUptime] returns the uptime in seconds, which is not a replacement for UpTime(). mach_absolute_time() should be a easy replacement for UpTime().

As for AbsoluteDeltaToNanoseconds(), you can just write the code to calculate the absolute difference between the timestamps, and I believe mach_absolute_time() is already nanoseconds on macOS (but you should verify that). Then, as long as you properly cast values to (uint64_t) when doing the division, you should have the same precision in the float result.

Brendan Shanks
  • 3,141
  • 14
  • 13
  • 2
    Note that `mach_absolute_time()` is no longer nanoseconds on Apple Silicon (for people who find this years later, like me…). – Wevah Jul 30 '22 at 22:41
  • 1
    Following from the previous comment by @Wevah: You should now be using clock_gettime_nsec_np(CLOCK_UPTIME_RAW) as Apple recommends it on the mach_absolute_time() page. That will give you uptime in ns consistently on Apple Silicon too. – G.Rassovsky May 10 '23 at 10:44