Skip to content

Commit 84069ce

Browse files
committed
bench: prefer a steady clock if the resolution is no worse
- backports bitcoin/bitcoin@24a0bdd
1 parent 38367b1 commit 84069ce

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/bench/bench.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ BENCHMARK(CODE_TO_TIME);
3636

3737
namespace benchmark {
3838

39-
using clock = std::chrono::high_resolution_clock;
39+
// On many systems, the high_resolution_clock offers no better resolution than the steady_clock.
40+
// If that's the case, prefer the steady_clock.
41+
struct best_clock {
42+
using hi_res_clock = std::chrono::high_resolution_clock;
43+
using steady_clock = std::chrono::steady_clock;
44+
static constexpr bool steady_is_high_res = std::ratio_less_equal<steady_clock::period, hi_res_clock::period>::value;
45+
using type = std::conditional<steady_is_high_res, steady_clock, hi_res_clock>::type;
46+
};
47+
using clock = best_clock::type;
4048
using time_point = clock::time_point;
4149
using duration = clock::duration;
4250

0 commit comments

Comments
 (0)