Skip to content

Commit

Permalink
Use clock_gettime instead of gettimeofday, it's more effiency on low …
Browse files Browse the repository at this point in the history
…power system

Add def for mac(They don't support clock_gettime)

Fix my mistake

Fix my mistake 2
  • Loading branch information
Mullin committed Feb 18, 2015
1 parent bff230f commit 797fc14
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Source/Core/Common/Timer.cpp
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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();
Expand All @@ -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;

Expand Down

0 comments on commit 797fc14

Please sign in to comment.