From 797fc14414f794a7bdc79fd4f6b9920e76a3c9c6 Mon Sep 17 00:00:00 2001 From: Mullin Date: Wed, 28 Jan 2015 20:28:01 +0100 Subject: [PATCH] Use clock_gettime instead of gettimeofday, it's more effiency on low power system Add def for mac(They don't support clock_gettime) Fix my mistake Fix my mistake 2 --- Source/Core/Common/Timer.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index 025fddeba6..d1da786a52 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -25,10 +25,14 @@ u32 Timer::GetTimeMs() { #ifdef _WIN32 return timeGetTime(); -#else +#elif defined __APPLE__ struct timeval t; (void)gettimeofday(&t, nullptr); return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000)); +#else + struct timespec t; + (void)clock_gettime(CLOCK_MONOTONIC, &t); + return ((u32)(t.tv_sec * 1000 + t.tv_nsec / 1000000)); #endif } @@ -48,10 +52,14 @@ u64 Timer::GetTimeUs() static double freq = GetFreq(); QueryPerformanceCounter(&time); return u64(double(time.QuadPart) * freq); -#else +#elif defined __APPLE__ struct timeval t; (void)gettimeofday(&t, nullptr); return ((u64)(t.tv_sec * 1000000 + t.tv_usec)); +#else + struct timespec t; + (void)clock_gettime(CLOCK_MONOTONIC, &t); + return ((u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000)); #endif } @@ -205,10 +213,14 @@ std::string Timer::GetTimeFormatted() struct timeb tp; (void)::ftime(&tp); return StringFromFormat("%s:%03i", tmp, tp.millitm); -#else +#elif defined __APPLE__ struct timeval t; (void)gettimeofday(&t, nullptr); return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000)); +#else + struct timespec t; + (void)clock_gettime(CLOCK_MONOTONIC, &t); + return StringFromFormat("%s:%03d", tmp, (int)(t.tv_nsec / 1000000)); #endif } @@ -219,9 +231,12 @@ double Timer::GetDoubleTime() #ifdef _WIN32 struct timeb tp; (void)::ftime(&tp); -#else +#elif defined __APPLE__ struct timeval t; (void)gettimeofday(&t, nullptr); +#else + struct timespec t; + (void)clock_gettime(CLOCK_MONOTONIC, &t); #endif // Get continuous timestamp u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); @@ -236,8 +251,10 @@ double Timer::GetDoubleTime() u32 Seconds = (u32)TmpSeconds; #ifdef _WIN32 double ms = tp.millitm / 1000.0 / 1000.0; -#else +#elif defined __APPLE__ double ms = t.tv_usec / 1000000.0; +#else + double ms = t.tv_nsec / 1000000000.0; #endif double TmpTime = Seconds + ms;