catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Use std::chrono for Timer to eliminate platform dependency
- Commit
242022460d5b466358fcab5e9ca884f2f7d99c6a- Author
- Phil Nash ·
- Parents
67005d290cac
Changes
include/internal/catch_timer.h | 4 ++-- include/internal/catch_timer.hpp | 34 +++++----------------------------- 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/include/internal/catch_timer.h b/include/internal/catch_timer.h index 6e8cf089..81b9fdb7 100644 --- a/include/internal/catch_timer.h +++ b/include/internal/catch_timer.h @@ -13,14 +13,14 @@ namespace Catch { class Timer { public: - Timer() : m_ticks( 0 ) {} + Timer() : m_microSeconds( 0 ) {} void start(); unsigned int getElapsedMicroseconds() const; unsigned int getElapsedMilliseconds() const; double getElapsedSeconds() const; private: - uint64_t m_ticks; + uint64_t m_microSeconds; }; } // namespace Catch diff --git a/include/internal/catch_timer.hpp b/include/internal/catch_timer.hpp index 52de3e83..d4066fa9 100644 --- a/include/internal/catch_timer.hpp +++ b/include/internal/catch_timer.hpp @@ -7,46 +7,22 @@ */ #include "catch_timer.h" -#include "catch_platform.h" -#ifdef CATCH_PLATFORM_WINDOWS - -# include "catch_windows_h_proxy.h" - -#else - -#include <sys/time.h> - -#endif +#include <chrono> namespace Catch { namespace { -#ifdef CATCH_PLATFORM_WINDOWS - uint64_t getCurrentTicks() { - static uint64_t hz=0, hzo=0; - if (!hz) { - QueryPerformanceFrequency( reinterpret_cast<LARGE_INTEGER*>( &hz ) ); - QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &hzo ) ); - } - uint64_t t; - QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &t ) ); - return ((t-hzo)*1000000)/hz; - } -#else - uint64_t getCurrentTicks() { - timeval t; - gettimeofday(&t,nullptr); - return static_cast<uint64_t>( t.tv_sec ) * 1000000ull + static_cast<uint64_t>( t.tv_usec ); + uint64_t getCurrentMicrosecondsSinceEpoch() { + return std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count(); } -#endif } void Timer::start() { - m_ticks = getCurrentTicks(); + m_microSeconds = getCurrentMicrosecondsSinceEpoch(); } unsigned int Timer::getElapsedMicroseconds() const { - return static_cast<unsigned int>(getCurrentTicks() - m_ticks); + return static_cast<unsigned int>(getCurrentMicrosecondsSinceEpoch() - m_microSeconds); } unsigned int Timer::getElapsedMilliseconds() const { return static_cast<unsigned int>(getElapsedMicroseconds()/1000);