diff --git a/StopWatch.cpp b/StopWatch.cpp index 77d539d..046a943 100644 --- a/StopWatch.cpp +++ b/StopWatch.cpp @@ -64,21 +64,16 @@ CStopWatch::~CStopWatch() unsigned long CStopWatch::start() { - ::gettimeofday(&m_start, NULL); - - return m_start.tv_usec; + ::clock_gettime(CLOCK_MONOTONIC, &m_start); + return m_start.tv_sec; } unsigned int CStopWatch::elapsed() { - struct timeval now; - ::gettimeofday(&now, NULL); - - unsigned int elapsed = (now.tv_sec - m_start.tv_sec) * 1000U; - elapsed += now.tv_usec / 1000U; - elapsed -= m_start.tv_usec / 1000U; - - return elapsed; + struct timespec now; + ::clock_gettime(CLOCK_MONOTONIC, &now); + int offset = ((now.tv_sec - m_start.tv_sec) * 1000000000UL + now.tv_nsec - m_start.tv_nsec ) / 1000000UL; + return (unsigned int)offset; } #endif diff --git a/StopWatch.h b/StopWatch.h index 811047e..a1e2bf8 100644 --- a/StopWatch.h +++ b/StopWatch.h @@ -39,7 +39,7 @@ private: LARGE_INTEGER m_frequency; LARGE_INTEGER m_start; #else - struct timeval m_start; + struct timespec m_start; #endif };