From cbc165f22f20ae1cb9c97fe80377512fefa09d80 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sun, 24 Jan 2016 21:18:53 +0000 Subject: [PATCH] Use gettimeofday on Linux for the StopWatch. --- StopWatch.cpp | 20 ++++++++------------ StopWatch.h | 8 ++++---- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/StopWatch.cpp b/StopWatch.cpp index f3279b4..c2ba6b6 100644 --- a/StopWatch.cpp +++ b/StopWatch.cpp @@ -62,24 +62,20 @@ CStopWatch::~CStopWatch() 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() { - timespec now; - ::clock_gettime(CLOCK_MONOTONIC, &now); + struct timeval now; + ::gettimeofday(&now, NULL); - if (m_start.tv_sec == now.tv_sec) { - return (now.tv_nsec - m_start.tv_nsec) / 1000000U; - } else { - long temp = -m_start.tv_nsec / 1000000L; - temp += (now.tv_sec - m_start.tv_sec) * 1000L; - temp += now.tv_nsec / 1000000L; - return temp; - } + unsigned long long a = m_start.tv_sec * 1000ULL + m_start.tv_usec / 1000ULL; + unsigned long long b = now.tv_sec * 1000ULL + now.tv_usec / 1000ULL; + + return (unsigned int)(b - a); } #endif diff --git a/StopWatch.h b/StopWatch.h index 4b44cb6..811047e 100644 --- a/StopWatch.h +++ b/StopWatch.h @@ -22,7 +22,7 @@ #if defined(_WIN32) || defined(_WIN64) #include #else -#include +#include #endif class CStopWatch @@ -36,10 +36,10 @@ public: private: #if defined(_WIN32) || defined(_WIN64) - LARGE_INTEGER m_frequency; - LARGE_INTEGER m_start; + LARGE_INTEGER m_frequency; + LARGE_INTEGER m_start; #else - timespec m_start; + struct timeval m_start; #endif };