Skip to content

Commit 24a0bdd

Browse files
committed
bench: prefer a steady clock if the resolution is no worse
1 parent c515d26 commit 24a0bdd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/bench/bench.h

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

3939
namespace benchmark {
40-
41-
using clock = std::chrono::high_resolution_clock;
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.
42+
struct best_clock {
43+
using hi_res_clock = std::chrono::high_resolution_clock;
44+
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;
47+
};
48+
using clock = best_clock::type;
4249
using time_point = clock::time_point;
4350
using duration = clock::duration;
4451

0 commit comments

Comments
 (0)