Merge pull request #1979 from Mullin/head

Use clock_gettime instead of gettimeofday, it's more efficiency on low power system
This commit is contained in:
Markus Wick 2015-02-18 21:54:16 +01:00
commit 86226cb2b9
1 changed files with 22 additions and 5 deletions

View File

@ -25,10 +25,14 @@ u32 Timer::GetTimeMs()
{ {
#ifdef _WIN32 #ifdef _WIN32
return timeGetTime(); return timeGetTime();
#else #elif defined __APPLE__
struct timeval t; struct timeval t;
(void)gettimeofday(&t, nullptr); (void)gettimeofday(&t, nullptr);
return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000)); 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 #endif
} }
@ -48,10 +52,14 @@ u64 Timer::GetTimeUs()
static double freq = GetFreq(); static double freq = GetFreq();
QueryPerformanceCounter(&time); QueryPerformanceCounter(&time);
return u64(double(time.QuadPart) * freq); return u64(double(time.QuadPart) * freq);
#else #elif defined __APPLE__
struct timeval t; struct timeval t;
(void)gettimeofday(&t, nullptr); (void)gettimeofday(&t, nullptr);
return ((u64)(t.tv_sec * 1000000 + t.tv_usec)); 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 #endif
} }
@ -205,10 +213,14 @@ std::string Timer::GetTimeFormatted()
struct timeb tp; struct timeb tp;
(void)::ftime(&tp); (void)::ftime(&tp);
return StringFromFormat("%s:%03i", tmp, tp.millitm); return StringFromFormat("%s:%03i", tmp, tp.millitm);
#else #elif defined __APPLE__
struct timeval t; struct timeval t;
(void)gettimeofday(&t, nullptr); (void)gettimeofday(&t, nullptr);
return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000)); 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 #endif
} }
@ -219,9 +231,12 @@ double Timer::GetDoubleTime()
#ifdef _WIN32 #ifdef _WIN32
struct timeb tp; struct timeb tp;
(void)::ftime(&tp); (void)::ftime(&tp);
#else #elif defined __APPLE__
struct timeval t; struct timeval t;
(void)gettimeofday(&t, nullptr); (void)gettimeofday(&t, nullptr);
#else
struct timespec t;
(void)clock_gettime(CLOCK_MONOTONIC, &t);
#endif #endif
// Get continuous timestamp // Get continuous timestamp
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970();
@ -236,8 +251,10 @@ double Timer::GetDoubleTime()
u32 Seconds = (u32)TmpSeconds; u32 Seconds = (u32)TmpSeconds;
#ifdef _WIN32 #ifdef _WIN32
double ms = tp.millitm / 1000.0 / 1000.0; double ms = tp.millitm / 1000.0 / 1000.0;
#else #elif defined __APPLE__
double ms = t.tv_usec / 1000000.0; double ms = t.tv_usec / 1000000.0;
#else
double ms = t.tv_nsec / 1000000000.0;
#endif #endif
double TmpTime = Seconds + ms; double TmpTime = Seconds + ms;