diff --git a/src/engine/Module.cpp b/src/engine/Module.cpp index c0ebd949..ca54191a 100644 --- a/src/engine/Module.cpp +++ b/src/engine/Module.cpp @@ -16,7 +16,7 @@ namespace engine { // Arbitrary prime number so it doesn't over- or under-estimate time of buffered processors. static const int METER_DIVIDER = 37; static const int METER_BUFFER_LEN = 32; -static const float METER_TIME = 1.0f; +static const float METER_TIME = 0.5f; struct Module::Internal { diff --git a/src/system.cpp b/src/system.cpp index c29fa637..c01678d9 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -19,8 +19,7 @@ #if defined ARCH_MAC #include #include - #include - #include + #include #include #endif @@ -660,9 +659,13 @@ std::string getStackTrace() { static int64_t startCounter = 0; static double counterTime = 0.0; #endif -#if defined ARCH_LIN || defined ARCH_MAC +#if defined ARCH_LIN static int64_t startTime = 0; #endif +#if defined ARCH_MAC + static int64_t startCounter = 0; + static double counterTime = 0.0; +#endif static void initTime() { #if defined ARCH_WIN @@ -680,12 +683,11 @@ static void initTime() { startTime = int64_t(ts.tv_sec) * 1000000000LL + ts.tv_nsec; #endif #if defined ARCH_MAC - clock_serv_t cclock; - mach_timespec_t mts; - host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - startTime = int64_t(mts.tv_sec) * 1000000000LL + mts.tv_nsec; + startCounter = mach_absolute_time(); + + mach_timebase_info_data_t tb; + mach_timebase_info(&tb); + counterTime = 1e-9 * (double) tb.numer / tb.denom; #endif } @@ -703,13 +705,9 @@ double getTime() { return (time - startTime) / 1e9; #endif #if defined ARCH_MAC - clock_serv_t cclock; - mach_timespec_t mts; - host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - int64_t time = int64_t(mts.tv_sec) * 1000000000LL + mts.tv_nsec; - return (time - startTime) / 1e9; + // Similar to mach_absolute_time() except it doesn't call the kernel every time. + int64_t counter = mach_approximate_time(); + return (counter - startCounter) * counterTime; #endif }