Skip to content

Commit 620bae3

Browse files
committed
Require a steady clock for bench with at least micro precision
1 parent 9e9e31a commit 620bae3

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/bench/bench.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void
2323
benchmark::BenchRunner::RunAll(benchmark::duration elapsedTimeForOne)
2424
{
2525
perf_init();
26+
if (std::ratio_less_equal<benchmark::clock::period, std::micro>::value) {
27+
std::cerr << "WARNING: Clock precision is worse than microsecond - benchmarks may be less accurate!\n";
28+
}
2629
std::cout << "#Benchmark" << "," << "count" << "," << "min(ns)" << "," << "max(ns)" << "," << "average(ns)" << ","
2730
<< "min_cycles" << "," << "max_cycles" << "," << "average_cycles" << "\n";
2831

src/bench/bench.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ BENCHMARK(CODE_TO_TIME);
3737
*/
3838

3939
namespace benchmark {
40-
// On many systems, the high_resolution_clock offers no better resolution than the steady_clock.
41-
// If that's the case, prefer the steady_clock.
40+
// In case high_resolution_clock is steady, prefer that, otherwise use steady_clock.
4241
struct best_clock {
4342
using hi_res_clock = std::chrono::high_resolution_clock;
4443
using steady_clock = std::chrono::steady_clock;
45-
static constexpr bool steady_is_high_res = std::ratio_less_equal<steady_clock::period, hi_res_clock::period>::value;
46-
using type = std::conditional<steady_is_high_res, steady_clock, hi_res_clock>::type;
44+
using type = std::conditional<hi_res_clock::is_steady, hi_res_clock, steady_clock>::type;
4745
};
4846
using clock = best_clock::type;
4947
using time_point = clock::time_point;

0 commit comments

Comments
 (0)