How to get NSTimeInterval value from last boot

I need to get the NSTimeInterval value from the last device boot. I found CACurrentMediaTime () that is suitable for this task, but in my application I do not use Core Animation, and I do not think that this is the best way to enable this structure in order to get this function. Is there any other way to get the time in seconds from the last boot in a more elegant way?

+4
source share
3 answers

The value is NSTimeInterval , since the last system restart can be obtained more directly through the following Foundation object and method:

[[NSProcessInfo processInfo] systemUptime]

+17
source

The fastest low-level method is to read system runtime from the processor using mach_absolute_time ()

 #include <mach/mach_time.h> int systemUptime() { static float timebase_ratio; if (timebase_ratio == 0) { mach_timebase_info_data_t s_timebase_info; (void) mach_timebase_info(&s_timebase_info); timebase_ratio = (float)s_timebase_info.numer / s_timebase_info.denom; } return (int)(timebase_ratio * mach_absolute_time() / 1000000000); } 

Note that timebase_ratio is different for processors. For example, on a macbook it is 1, whereas on an iPhone 5 it is 125/3 (~ 40).

+1
source

Try C system call, time (3) should return uptime.

On MacOSX, uptime also returns this. So there must also be a way.

-1
source

All Articles