Use gettimeofday on Linux for the StopWatch.

This commit is contained in:
Jonathan Naylor 2016-01-24 21:18:53 +00:00
parent 6004b086e3
commit cbc165f22f
2 changed files with 12 additions and 16 deletions

View file

@ -62,24 +62,20 @@ CStopWatch::~CStopWatch()
unsigned long CStopWatch::start() unsigned long CStopWatch::start()
{ {
::clock_gettime(CLOCK_MONOTONIC, &m_start); ::gettimeofday(&m_start, NULL);
return m_start.tv_nsec; return m_start.tv_usec;
} }
unsigned int CStopWatch::elapsed() unsigned int CStopWatch::elapsed()
{ {
timespec now; struct timeval now;
::clock_gettime(CLOCK_MONOTONIC, &now); ::gettimeofday(&now, NULL);
if (m_start.tv_sec == now.tv_sec) { unsigned long long a = m_start.tv_sec * 1000ULL + m_start.tv_usec / 1000ULL;
return (now.tv_nsec - m_start.tv_nsec) / 1000000U; unsigned long long b = now.tv_sec * 1000ULL + now.tv_usec / 1000ULL;
} else {
long temp = -m_start.tv_nsec / 1000000L; return (unsigned int)(b - a);
temp += (now.tv_sec - m_start.tv_sec) * 1000L;
temp += now.tv_nsec / 1000000L;
return temp;
}
} }
#endif #endif

View file

@ -22,7 +22,7 @@
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#include <windows.h> #include <windows.h>
#else #else
#include <ctime> #include <sys/time.h>
#endif #endif
class CStopWatch class CStopWatch
@ -36,10 +36,10 @@ public:
private: private:
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
LARGE_INTEGER m_frequency; LARGE_INTEGER m_frequency;
LARGE_INTEGER m_start; LARGE_INTEGER m_start;
#else #else
timespec m_start; struct timeval m_start;
#endif #endif
}; };