From 51d534034b0ac372a65ee73b98ca09877cda5d0a Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sun, 15 Jul 2018 20:30:47 +0100 Subject: [PATCH] Simplify the new stopwatch code. --- YSFGateway/StopWatch.cpp | 44 ++++++++++++++++++++++++++++---------- YSFGateway/StopWatch.h | 11 ++++++---- YSFParrot/StopWatch.cpp | 44 ++++++++++++++++++++++++++++---------- YSFParrot/StopWatch.h | 11 ++++++---- YSFReflector/StopWatch.cpp | 44 ++++++++++++++++++++++++++++---------- YSFReflector/StopWatch.h | 11 ++++++---- 6 files changed, 120 insertions(+), 45 deletions(-) diff --git a/YSFGateway/StopWatch.cpp b/YSFGateway/StopWatch.cpp index eb32961..481241b 100644 --- a/YSFGateway/StopWatch.cpp +++ b/YSFGateway/StopWatch.cpp @@ -21,21 +21,32 @@ #if defined(_WIN32) || defined(_WIN64) CStopWatch::CStopWatch() : -m_frequency(), +m_frequencyS(), +m_frequencyMS(), m_start() { - ::QueryPerformanceFrequency(&m_frequency); + ::QueryPerformanceFrequency(&m_frequencyS); + + m_frequencyMS.QuadPart = m_frequencyS.QuadPart / 1000ULL; } CStopWatch::~CStopWatch() { } -unsigned long CStopWatch::start() +unsigned long long CStopWatch::time() const +{ + LARGE_INTEGER now; + ::QueryPerformanceCounter(&now); + + return (unsigned long long)(now.QuadPart / m_frequencyMS.QuadPart); +} + +unsigned long long CStopWatch::start() { ::QueryPerformanceCounter(&m_start); - return (unsigned long)(m_start.QuadPart / m_frequency.QuadPart); + return (unsigned long long)(m_start.QuadPart / m_frequencyS.QuadPart); } unsigned int CStopWatch::elapsed() @@ -46,7 +57,7 @@ unsigned int CStopWatch::elapsed() LARGE_INTEGER temp; temp.QuadPart = (now.QuadPart - m_start.QuadPart) * 1000; - return (unsigned int)(temp.QuadPart / m_frequency.QuadPart); + return (unsigned int)(temp.QuadPart / m_frequencyS.QuadPart); } #else @@ -55,7 +66,7 @@ unsigned int CStopWatch::elapsed() #include CStopWatch::CStopWatch() : -m_start() +m_startMS(0ULL) { } @@ -63,11 +74,22 @@ CStopWatch::~CStopWatch() { } -unsigned long CStopWatch::start() +unsigned long long CStopWatch::time() const { - ::clock_gettime(CLOCK_MONOTONIC, &m_start); + struct timeval now; + ::gettimeofday(&now, NULL); + + return now.tv_sec * 1000ULL + now.tv_usec / 1000ULL; +} + +unsigned long long CStopWatch::start() +{ + struct timespec now; + ::clock_gettime(CLOCK_MONOTONIC, &now); + + m_startMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL; - return m_start.tv_sec * 1000UL + m_start.tv_nsec / 1000000UL; + return m_startMS; } unsigned int CStopWatch::elapsed() @@ -75,9 +97,9 @@ unsigned int CStopWatch::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; + unsigned long long nowMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL; - return (unsigned int)offset; + return nowMS - m_startMS; } #endif diff --git a/YSFGateway/StopWatch.h b/YSFGateway/StopWatch.h index 554f91c..3f8fa19 100644 --- a/YSFGateway/StopWatch.h +++ b/YSFGateway/StopWatch.h @@ -31,15 +31,18 @@ public: CStopWatch(); ~CStopWatch(); - unsigned long start(); - unsigned int elapsed(); + unsigned long long time() const; + + unsigned long long start(); + unsigned int elapsed(); private: #if defined(_WIN32) || defined(_WIN64) - LARGE_INTEGER m_frequency; + LARGE_INTEGER m_frequencyS; + LARGE_INTEGER m_frequencyMS; LARGE_INTEGER m_start; #else - struct timespec m_start; + unsigned long long m_startMS; #endif }; diff --git a/YSFParrot/StopWatch.cpp b/YSFParrot/StopWatch.cpp index eb32961..481241b 100644 --- a/YSFParrot/StopWatch.cpp +++ b/YSFParrot/StopWatch.cpp @@ -21,21 +21,32 @@ #if defined(_WIN32) || defined(_WIN64) CStopWatch::CStopWatch() : -m_frequency(), +m_frequencyS(), +m_frequencyMS(), m_start() { - ::QueryPerformanceFrequency(&m_frequency); + ::QueryPerformanceFrequency(&m_frequencyS); + + m_frequencyMS.QuadPart = m_frequencyS.QuadPart / 1000ULL; } CStopWatch::~CStopWatch() { } -unsigned long CStopWatch::start() +unsigned long long CStopWatch::time() const +{ + LARGE_INTEGER now; + ::QueryPerformanceCounter(&now); + + return (unsigned long long)(now.QuadPart / m_frequencyMS.QuadPart); +} + +unsigned long long CStopWatch::start() { ::QueryPerformanceCounter(&m_start); - return (unsigned long)(m_start.QuadPart / m_frequency.QuadPart); + return (unsigned long long)(m_start.QuadPart / m_frequencyS.QuadPart); } unsigned int CStopWatch::elapsed() @@ -46,7 +57,7 @@ unsigned int CStopWatch::elapsed() LARGE_INTEGER temp; temp.QuadPart = (now.QuadPart - m_start.QuadPart) * 1000; - return (unsigned int)(temp.QuadPart / m_frequency.QuadPart); + return (unsigned int)(temp.QuadPart / m_frequencyS.QuadPart); } #else @@ -55,7 +66,7 @@ unsigned int CStopWatch::elapsed() #include CStopWatch::CStopWatch() : -m_start() +m_startMS(0ULL) { } @@ -63,11 +74,22 @@ CStopWatch::~CStopWatch() { } -unsigned long CStopWatch::start() +unsigned long long CStopWatch::time() const { - ::clock_gettime(CLOCK_MONOTONIC, &m_start); + struct timeval now; + ::gettimeofday(&now, NULL); + + return now.tv_sec * 1000ULL + now.tv_usec / 1000ULL; +} + +unsigned long long CStopWatch::start() +{ + struct timespec now; + ::clock_gettime(CLOCK_MONOTONIC, &now); + + m_startMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL; - return m_start.tv_sec * 1000UL + m_start.tv_nsec / 1000000UL; + return m_startMS; } unsigned int CStopWatch::elapsed() @@ -75,9 +97,9 @@ unsigned int CStopWatch::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; + unsigned long long nowMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL; - return (unsigned int)offset; + return nowMS - m_startMS; } #endif diff --git a/YSFParrot/StopWatch.h b/YSFParrot/StopWatch.h index 554f91c..3f8fa19 100644 --- a/YSFParrot/StopWatch.h +++ b/YSFParrot/StopWatch.h @@ -31,15 +31,18 @@ public: CStopWatch(); ~CStopWatch(); - unsigned long start(); - unsigned int elapsed(); + unsigned long long time() const; + + unsigned long long start(); + unsigned int elapsed(); private: #if defined(_WIN32) || defined(_WIN64) - LARGE_INTEGER m_frequency; + LARGE_INTEGER m_frequencyS; + LARGE_INTEGER m_frequencyMS; LARGE_INTEGER m_start; #else - struct timespec m_start; + unsigned long long m_startMS; #endif }; diff --git a/YSFReflector/StopWatch.cpp b/YSFReflector/StopWatch.cpp index eb32961..481241b 100644 --- a/YSFReflector/StopWatch.cpp +++ b/YSFReflector/StopWatch.cpp @@ -21,21 +21,32 @@ #if defined(_WIN32) || defined(_WIN64) CStopWatch::CStopWatch() : -m_frequency(), +m_frequencyS(), +m_frequencyMS(), m_start() { - ::QueryPerformanceFrequency(&m_frequency); + ::QueryPerformanceFrequency(&m_frequencyS); + + m_frequencyMS.QuadPart = m_frequencyS.QuadPart / 1000ULL; } CStopWatch::~CStopWatch() { } -unsigned long CStopWatch::start() +unsigned long long CStopWatch::time() const +{ + LARGE_INTEGER now; + ::QueryPerformanceCounter(&now); + + return (unsigned long long)(now.QuadPart / m_frequencyMS.QuadPart); +} + +unsigned long long CStopWatch::start() { ::QueryPerformanceCounter(&m_start); - return (unsigned long)(m_start.QuadPart / m_frequency.QuadPart); + return (unsigned long long)(m_start.QuadPart / m_frequencyS.QuadPart); } unsigned int CStopWatch::elapsed() @@ -46,7 +57,7 @@ unsigned int CStopWatch::elapsed() LARGE_INTEGER temp; temp.QuadPart = (now.QuadPart - m_start.QuadPart) * 1000; - return (unsigned int)(temp.QuadPart / m_frequency.QuadPart); + return (unsigned int)(temp.QuadPart / m_frequencyS.QuadPart); } #else @@ -55,7 +66,7 @@ unsigned int CStopWatch::elapsed() #include CStopWatch::CStopWatch() : -m_start() +m_startMS(0ULL) { } @@ -63,11 +74,22 @@ CStopWatch::~CStopWatch() { } -unsigned long CStopWatch::start() +unsigned long long CStopWatch::time() const { - ::clock_gettime(CLOCK_MONOTONIC, &m_start); + struct timeval now; + ::gettimeofday(&now, NULL); + + return now.tv_sec * 1000ULL + now.tv_usec / 1000ULL; +} + +unsigned long long CStopWatch::start() +{ + struct timespec now; + ::clock_gettime(CLOCK_MONOTONIC, &now); + + m_startMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL; - return m_start.tv_sec * 1000UL + m_start.tv_nsec / 1000000UL; + return m_startMS; } unsigned int CStopWatch::elapsed() @@ -75,9 +97,9 @@ unsigned int CStopWatch::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; + unsigned long long nowMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL; - return (unsigned int)offset; + return nowMS - m_startMS; } #endif diff --git a/YSFReflector/StopWatch.h b/YSFReflector/StopWatch.h index 554f91c..3f8fa19 100644 --- a/YSFReflector/StopWatch.h +++ b/YSFReflector/StopWatch.h @@ -31,15 +31,18 @@ public: CStopWatch(); ~CStopWatch(); - unsigned long start(); - unsigned int elapsed(); + unsigned long long time() const; + + unsigned long long start(); + unsigned int elapsed(); private: #if defined(_WIN32) || defined(_WIN64) - LARGE_INTEGER m_frequency; + LARGE_INTEGER m_frequencyS; + LARGE_INTEGER m_frequencyMS; LARGE_INTEGER m_start; #else - struct timespec m_start; + unsigned long long m_startMS; #endif };