2013-08-07 19:56:35 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 05/08/2013.
|
|
|
|
* Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "catch_timer.h"
|
|
|
|
|
2017-04-25 13:24:34 +02:00
|
|
|
#include <chrono>
|
2013-08-07 19:56:35 +02:00
|
|
|
|
2018-04-01 22:50:39 +02:00
|
|
|
static const uint64_t nanosecondsInSecond = 1000000000;
|
|
|
|
|
2013-08-07 19:56:35 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2017-08-04 13:22:16 +02:00
|
|
|
auto getCurrentNanosecondsSinceEpoch() -> uint64_t {
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
|
2013-08-07 19:56:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
namespace {
|
|
|
|
auto estimateClockResolution() -> uint64_t {
|
|
|
|
uint64_t sum = 0;
|
|
|
|
static const uint64_t iterations = 1000000;
|
2017-08-04 20:23:30 +02:00
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
auto startTime = getCurrentNanosecondsSinceEpoch();
|
2018-04-01 22:50:39 +02:00
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
for( std::size_t i = 0; i < iterations; ++i ) {
|
2017-08-04 20:23:30 +02:00
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
uint64_t ticks;
|
|
|
|
uint64_t baseTicks = getCurrentNanosecondsSinceEpoch();
|
|
|
|
do {
|
|
|
|
ticks = getCurrentNanosecondsSinceEpoch();
|
|
|
|
} while( ticks == baseTicks );
|
2017-08-04 20:23:30 +02:00
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
auto delta = ticks - baseTicks;
|
|
|
|
sum += delta;
|
2018-04-01 22:50:39 +02:00
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
// If we have been calibrating for over 3 seconds -- the clock
|
|
|
|
// is terrible and we should move on.
|
|
|
|
// TBD: How to signal that the measured resolution is probably wrong?
|
|
|
|
if (ticks > startTime + 3 * nanosecondsInSecond) {
|
2019-01-07 18:26:28 +01:00
|
|
|
return sum / ( i + 1u );
|
2018-06-12 15:09:30 +02:00
|
|
|
}
|
2018-04-01 22:50:39 +02:00
|
|
|
}
|
2017-08-04 14:53:47 +02:00
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
// We're just taking the mean, here. To do better we could take the std. dev and exclude outliers
|
|
|
|
// - and potentially do more iterations if there's a high variance.
|
|
|
|
return sum/iterations;
|
|
|
|
}
|
2017-08-04 20:23:30 +02:00
|
|
|
}
|
2017-08-04 22:31:28 +02:00
|
|
|
auto getEstimatedClockResolution() -> uint64_t {
|
2017-08-04 20:23:30 +02:00
|
|
|
static auto s_resolution = estimateClockResolution();
|
|
|
|
return s_resolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::start() {
|
|
|
|
m_nanoseconds = getCurrentNanosecondsSinceEpoch();
|
|
|
|
}
|
2017-12-08 16:57:36 +01:00
|
|
|
auto Timer::getElapsedNanoseconds() const -> uint64_t {
|
|
|
|
return getCurrentNanosecondsSinceEpoch() - m_nanoseconds;
|
2017-08-04 20:23:30 +02:00
|
|
|
}
|
2017-12-08 16:57:36 +01:00
|
|
|
auto Timer::getElapsedMicroseconds() const -> uint64_t {
|
|
|
|
return getElapsedNanoseconds()/1000;
|
2017-08-04 20:23:30 +02:00
|
|
|
}
|
|
|
|
auto Timer::getElapsedMilliseconds() const -> unsigned int {
|
|
|
|
return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
|
|
|
|
}
|
|
|
|
auto Timer::getElapsedSeconds() const -> double {
|
|
|
|
return getElapsedMicroseconds()/1000000.0;
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:53:47 +02:00
|
|
|
|
2013-08-07 19:56:35 +02:00
|
|
|
} // namespace Catch
|