From 7b69c38da52d7ee0052c500edf5abc962afeff0e Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Fri, 31 Aug 2012 18:26:59 -0400 Subject: [PATCH] Switch OSX timer to real-time too. --- Core/src/StopWatchOSX.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Core/src/StopWatchOSX.cpp b/Core/src/StopWatchOSX.cpp index 2aeb971..fda23cc 100644 --- a/Core/src/StopWatchOSX.cpp +++ b/Core/src/StopWatchOSX.cpp @@ -3,24 +3,20 @@ #include #include -#include +#include class StopWatchImpl { public: uint64 start; - - double resolution; - double duration; - - StopWatchImpl() { - mach_timebase_info_data_t info; - mach_timebase_info(&info); - - resolution = double(info.numer) / double(info.denom); - resolution *= 1e-9; - } + uint64 duration; }; +static uint64 Now() { + timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_usec + (1e6 * tv.tv_sec); +} + StopWatch::StopWatch(const StopWatch &other) { impl = new StopWatchImpl(); memcpy(impl, other.impl, sizeof(StopWatchImpl)); @@ -43,11 +39,11 @@ StopWatch::StopWatch() : impl(new StopWatchImpl) { } void StopWatch::Start() { - impl->start = mach_absolute_time(); + impl->start = Now(); } void StopWatch::Stop() { - impl->duration = impl->resolution * (double(mach_absolute_time()) - impl->start); + impl->duration = Now() - impl->start; } void StopWatch::Reset() { @@ -55,13 +51,13 @@ void StopWatch::Reset() { } double StopWatch::TimeInSeconds() const { - return impl->duration; + return double(impl->duration) / 1e6; } double StopWatch::TimeInMilliseconds() const { - return impl->duration * 1000; + return double(impl->duration) / 1e3; } double StopWatch::TimeInMicroseconds() const { - return impl->duration * 1000000; + return double(impl->duration); }