aboutsummaryrefslogtreecommitdiffstats
path: root/test/bench_utils.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-08-26 23:16:00 +0200
committerDavid Robillard <d@drobilla.net>2018-09-29 14:50:34 +0200
commit0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580 (patch)
tree870b40f80575969ab5bb7771d62ecb0bdf13662a /test/bench_utils.hpp
parenta115beb0d3fedbe3d286ad1ba2dd7af319f7968d (diff)
downloadchilbert-0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580.tar.gz
chilbert-0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580.tar.bz2
chilbert-0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580.zip
Add benchmarks
Diffstat (limited to 'test/bench_utils.hpp')
-rw-r--r--test/bench_utils.hpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/test/bench_utils.hpp b/test/bench_utils.hpp
new file mode 100644
index 0000000..a3f7f81
--- /dev/null
+++ b/test/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