diff options
author | David Robillard <d@drobilla.net> | 2022-09-16 15:39:48 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-09-16 22:31:06 -0400 |
commit | 49dab5622b31421eb6af84eae376d73fae1cd4a0 (patch) | |
tree | 86290707551320ab35952bccc11c66df05714b26 /benchmark/bench_utils.hpp | |
parent | 342a22b6d75597ee22c195b60607402e3ed028b2 (diff) | |
download | chilbert-49dab5622b31421eb6af84eae376d73fae1cd4a0.tar.gz chilbert-49dab5622b31421eb6af84eae376d73fae1cd4a0.tar.bz2 chilbert-49dab5622b31421eb6af84eae376d73fae1cd4a0.zip |
Switch to meson build system
Diffstat (limited to 'benchmark/bench_utils.hpp')
-rw-r--r-- | benchmark/bench_utils.hpp | 64 |
1 files changed, 64 insertions, 0 deletions
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 <d@drobilla.net> + + 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 <https://www.gnu.org/licenses/>. +*/ + +#ifndef BENCH_UTILS_HPP +#define BENCH_UTILS_HPP + +#include "test_utils.hpp" + +#include <array> +#include <chrono> +#include <cstddef> +#include <iostream> + +using Duration = std::chrono::duration<double, std::micro>; + +/// Write a TSV row to `os` with `n` as the first column followed by `results` +template <class T, size_t M> +void +write_row(std::ostream& os, const size_t n, const std::array<T, M>& 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 <class Operation> +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 |