From 620bae34cfe10e20daa0dcec7e4b9ffee8dfd397 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 9 Nov 2017 11:48:13 -0500 Subject: [PATCH] Require a steady clock for bench with at least micro precision --- src/bench/bench.cpp | 3 +++ src/bench/bench.h | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index dd4ba5ab0ee23..4c5a036773d8a 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -23,6 +23,9 @@ void benchmark::BenchRunner::RunAll(benchmark::duration elapsedTimeForOne) { perf_init(); + if (std::ratio_less_equal::value) { + std::cerr << "WARNING: Clock precision is worse than microsecond - benchmarks may be less accurate!\n"; + } std::cout << "#Benchmark" << "," << "count" << "," << "min(ns)" << "," << "max(ns)" << "," << "average(ns)" << "," << "min_cycles" << "," << "max_cycles" << "," << "average_cycles" << "\n"; diff --git a/src/bench/bench.h b/src/bench/bench.h index d276f4ee91f4e..ab5c3d5604d22 100644 --- a/src/bench/bench.h +++ b/src/bench/bench.h @@ -37,13 +37,11 @@ BENCHMARK(CODE_TO_TIME); */ namespace benchmark { - // On many systems, the high_resolution_clock offers no better resolution than the steady_clock. - // If that's the case, prefer the steady_clock. + // In case high_resolution_clock is steady, prefer that, otherwise use steady_clock. struct best_clock { using hi_res_clock = std::chrono::high_resolution_clock; using steady_clock = std::chrono::steady_clock; - static constexpr bool steady_is_high_res = std::ratio_less_equal::value; - using type = std::conditional::type; + using type = std::conditional::type; }; using clock = best_clock::type; using time_point = clock::time_point;