Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include/internal/catch_timer.cpp
diff options
context:
space:
mode:
authorPhil Nash2017-08-04 19:23:30 +0100
committerPhil Nash2017-08-04 19:23:30 +0100
commita9b6813ad9e423ddcd6559f52b503a68fe0f624c (patch)
tree2855a5fdcae2f2bf20535b33a8a697975a92896b /include/internal/catch_timer.cpp
parenta1e3f0b62446afe9b46b0872d373ab0e4e8a4357 (diff)
downloadcatch3-a9b6813ad9e423ddcd6559f52b503a68fe0f624c.tar.gz
First draft of (experimental) benchmarking support
Diffstat (limited to 'include/internal/catch_timer.cpp')
-rw-r--r--include/internal/catch_timer.cpp54
1 files changed, 40 insertions, 14 deletions
diff --git a/include/internal/catch_timer.cpp b/include/internal/catch_timer.cpp
index ca09d7bc..9635ccea 100644
--- a/include/internal/catch_timer.cpp
+++ b/include/internal/catch_timer.cpp
@@ -16,21 +16,47 @@ namespace Catch {
return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
}
- void Timer::start() {
- m_nanoseconds = getCurrentNanosecondsSinceEpoch();
- }
- auto Timer::getElapsedNanoseconds() const -> unsigned int {
- return static_cast<unsigned int>(getCurrentNanosecondsSinceEpoch() - m_nanoseconds);
- }
- auto Timer::getElapsedMicroseconds() const -> unsigned int {
- return static_cast<unsigned int>(getElapsedNanoseconds()/1000);
- }
- auto Timer::getElapsedMilliseconds() const -> unsigned int {
- return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
- }
- auto Timer::getElapsedSeconds() const -> double {
- return getElapsedMicroseconds()/1000000.0;
+ auto estimateClockResolution() -> double {
+ uint64_t sum = 0;
+ static const uint64_t iterations = 1000000;
+
+ for( 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;
}
+ // 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/(double)iterations;
+ }
+ auto getEstimatedClockResolution() -> double {
+ static auto s_resolution = estimateClockResolution();
+ return s_resolution;
+ }
+
+ void Timer::start() {
+ m_nanoseconds = getCurrentNanosecondsSinceEpoch();
+ }
+ auto Timer::getElapsedNanoseconds() const -> unsigned int {
+ return static_cast<unsigned int>(getCurrentNanosecondsSinceEpoch() - m_nanoseconds);
+ }
+ auto Timer::getElapsedMicroseconds() const -> unsigned int {
+ return static_cast<unsigned int>(getElapsedNanoseconds()/1000);
+ }
+ auto Timer::getElapsedMilliseconds() const -> unsigned int {
+ return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
+ }
+ auto Timer::getElapsedSeconds() const -> double {
+ return getElapsedMicroseconds()/1000000.0;
+ }
+
} // namespace Catch