How to get NSTimeInterval value from last boot

前端 未结 3 358
一整个雨季
一整个雨季 2020-12-19 09:35

I need to get NSTimeInterval value from last device boot. I found CACurrentMediaTime() which suits this task, but in my app I am not using Core Animation and I don\'t think

相关标签:
3条回答
  • 2020-12-19 09:48

    The fastest low-level method is to read system uptime from 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 macbook it equals 1 whereas on iPhone 5 it equals 125/3 (~40).

    0 讨论(0)
  • 2020-12-19 09:50

    The NSTimeInterval value since the last system restart can be acquired more directly via the following Foundation object and method:

    [[NSProcessInfo processInfo] systemUptime]

    0 讨论(0)
  • 2020-12-19 10:11

    Try a C system call, times(3) is supposed to return uptime.

    On MacOSX, uptime also returns such. So there has to be a way though that as well.

    0 讨论(0)
提交回复
热议问题