From 49dab5622b31421eb6af84eae376d73fae1cd4a0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 16 Sep 2022 15:39:48 -0400 Subject: Switch to meson build system --- benchmark/bench_utils.hpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 benchmark/bench_utils.hpp (limited to 'benchmark/bench_utils.hpp') diff --git a/benchmark/bench_utils.hpp b/benchmark/bench_utils.hpp new file mode 100644 index 0000000..a3f7f81 --- /dev/null +++ b/benchmark/bench_utils.hpp @@ -0,0 +1,64 @@ +/* + Copyright (C) 2018 David Robillard + + This program is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free Software + Foundation, either version 2 of the License, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + this program. If not, see . +*/ + +#ifndef BENCH_UTILS_HPP +#define BENCH_UTILS_HPP + +#include "test_utils.hpp" + +#include +#include +#include +#include + +using Duration = std::chrono::duration; + +/// Write a TSV row to `os` with `n` as the first column followed by `results` +template +void +write_row(std::ostream& os, const size_t n, const std::array& results) +{ + os << n; + for (const auto t : results) { + if (t == Duration::zero()) { + os << "\tNaN"; + } else { + os << '\t' << t.count(); + } + } + os << std::endl; +} + +/// Repeatedly run an operation and return the average time +template +Duration +run_bench(const Operation& op) +{ + static constexpr auto bench_duration = std::chrono::milliseconds{10}; + + const auto t_start = std::chrono::steady_clock{}.now(); + auto t_now = t_start; + size_t count = 0; + for (; t_now < t_start + bench_duration; ++count) { + op(count); + t_now = std::chrono::steady_clock{}.now(); + } + + return (t_now - t_start) / count; +} + +#endif -- cgit v1.2.1