1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "catch_timer.h"
- #include <chrono>
- static const uint64_t nanosecondsInSecond = 1000000000;
- namespace Catch {
- auto getCurrentNanosecondsSinceEpoch() -> uint64_t {
- return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
- }
- namespace {
- auto estimateClockResolution() -> uint64_t {
- uint64_t sum = 0;
- static const uint64_t iterations = 1000000;
- auto startTime = getCurrentNanosecondsSinceEpoch();
- for( std::size_t i = 0; i < iterations; ++i ) {
- uint64_t ticks;
- uint64_t baseTicks = getCurrentNanosecondsSinceEpoch();
- do {
- ticks = getCurrentNanosecondsSinceEpoch();
- } while( ticks == baseTicks );
- auto delta = ticks - baseTicks;
- sum += delta;
-
-
-
- if (ticks > startTime + 3 * nanosecondsInSecond) {
- return sum / ( i + 1u );
- }
- }
-
-
- return sum/iterations;
- }
- }
- auto getEstimatedClockResolution() -> uint64_t {
- static auto s_resolution = estimateClockResolution();
- return s_resolution;
- }
- void Timer::start() {
- m_nanoseconds = getCurrentNanosecondsSinceEpoch();
- }
- auto Timer::getElapsedNanoseconds() const -> uint64_t {
- return getCurrentNanosecondsSinceEpoch() - m_nanoseconds;
- }
- auto Timer::getElapsedMicroseconds() const -> uint64_t {
- return getElapsedNanoseconds()/1000;
- }
- auto Timer::getElapsedMilliseconds() const -> unsigned int {
- return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
- }
- auto Timer::getElapsedSeconds() const -> double {
- return getElapsedMicroseconds()/1000000.0;
- }
- }
|