Use std::chrono for Timer to eliminate platform dependency

This commit is contained in:
Phil Nash 2017-04-25 12:24:34 +01:00
parent 67005d290c
commit 242022460d
2 changed files with 7 additions and 31 deletions

View File

@ -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

View File

@ -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 getCurrentMicrosecondsSinceEpoch() {
return std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
}
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 );
}
#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);