From 657ef07b56291bb93036a24c211fdfb7d2eca1d5 Mon Sep 17 00:00:00 2001 From: Shawn Chain Date: Wed, 4 Jul 2018 17:54:38 +0800 Subject: [PATCH] Fix time measurement issud --- StopWatch.cpp | 17 ++++++----------- StopWatch.h | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) 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 };