mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-04 05:09:53 +01:00
Use std::chrono for Timer to eliminate platform dependency
This commit is contained in:
parent
67005d290c
commit
242022460d
@ -13,14 +13,14 @@
|
|||||||
namespace Catch {
|
namespace Catch {
|
||||||
class Timer {
|
class Timer {
|
||||||
public:
|
public:
|
||||||
Timer() : m_ticks( 0 ) {}
|
Timer() : m_microSeconds( 0 ) {}
|
||||||
void start();
|
void start();
|
||||||
unsigned int getElapsedMicroseconds() const;
|
unsigned int getElapsedMicroseconds() const;
|
||||||
unsigned int getElapsedMilliseconds() const;
|
unsigned int getElapsedMilliseconds() const;
|
||||||
double getElapsedSeconds() const;
|
double getElapsedSeconds() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t m_ticks;
|
uint64_t m_microSeconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Catch
|
} // namespace Catch
|
||||||
|
@ -7,46 +7,22 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "catch_timer.h"
|
#include "catch_timer.h"
|
||||||
#include "catch_platform.h"
|
|
||||||
|
|
||||||
#ifdef CATCH_PLATFORM_WINDOWS
|
#include <chrono>
|
||||||
|
|
||||||
# include "catch_windows_h_proxy.h"
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
#ifdef CATCH_PLATFORM_WINDOWS
|
uint64_t getCurrentMicrosecondsSinceEpoch() {
|
||||||
uint64_t getCurrentTicks() {
|
return std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
|
||||||
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 );
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::start() {
|
void Timer::start() {
|
||||||
m_ticks = getCurrentTicks();
|
m_microSeconds = getCurrentMicrosecondsSinceEpoch();
|
||||||
}
|
}
|
||||||
unsigned int Timer::getElapsedMicroseconds() const {
|
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 {
|
unsigned int Timer::getElapsedMilliseconds() const {
|
||||||
return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
|
return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
|
||||||
|
Loading…
Reference in New Issue
Block a user