diff options
author | David Robillard <d@drobilla.net> | 2018-08-26 23:16:00 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-09-29 14:50:34 +0200 |
commit | 0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580 (patch) | |
tree | 870b40f80575969ab5bb7771d62ecb0bdf13662a /test/bench_hilbert.cpp | |
parent | a115beb0d3fedbe3d286ad1ba2dd7af319f7968d (diff) | |
download | chilbert-0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580.tar.gz chilbert-0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580.tar.bz2 chilbert-0a5a45ff8ae437ddf1c3b8de425f30a44e6f5580.zip |
Add benchmarks
Diffstat (limited to 'test/bench_hilbert.cpp')
-rw-r--r-- | test/bench_hilbert.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/test/bench_hilbert.cpp b/test/bench_hilbert.cpp new file mode 100644 index 0000000..c5f1ad9 --- /dev/null +++ b/test/bench_hilbert.cpp @@ -0,0 +1,111 @@ +/* + 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/>. +*/ + +#include "bench_utils.hpp" + +#include "chilbert/BoundedBitVec.hpp" +#include "chilbert/DynamicBitVec.hpp" +#include "chilbert/SmallBitVec.hpp" +#include "chilbert/StaticBitVec.hpp" +#include "chilbert/chilbert.hpp" + +#include <fstream> + +template <class H, size_t M, size_t D> +struct BenchCoordsToIndex +{ + Duration operator()(Context& ctx) + { + const auto p = make_random_point<M, D>(ctx); + H ha = make_zero_bitvec<H, D * M>(); + + return run_bench( + [&](auto) { chilbert::coords_to_index(p.data(), M, D, ha); }); + } +}; + +template <class H, size_t M, size_t D> +struct BenchIndexToCoords +{ + Duration operator()(Context& ctx) + { + auto p = make_random_point<M, D>(ctx); + const H ha = make_random_bitvec<H, D * M>(ctx); + + return run_bench( + [&](auto) { chilbert::index_to_coords(p.data(), M, D, ha); }); + } +}; + +/// Run benchmark for size N +template <template <class H, size_t M, size_t D> class Bench, + size_t M, + size_t D> +void +bench_row(Context& ctx, std::ostream& os) +{ + std::array<Duration, 4> results = { + ((M * D <= chilbert::SmallBitVec::bits_per_rack) + ? Bench<chilbert::SmallBitVec, M, D>{}(ctx) + : Duration{}), + Bench<chilbert::StaticBitVec<M * D>, M, D>{}(ctx), + Bench<chilbert::BoundedBitVec<M * D>, M, D>{}(ctx), + Bench<chilbert::DynamicBitVec, M, D>{}(ctx), + }; + + write_row(os, D, results); +} + +/// Terminate recursion +template <template <class H, size_t M, size_t D> class Bench, size_t M> +void +bench_rec(Context&, std::ostream&) +{ +} + +/// Run benchmark for sizes N, Ns... (recursive helper) +template <template <class H, size_t M, size_t D> class Bench, + size_t M, + size_t D, + size_t... Ds> +void +bench_rec(Context& ctx, std::ostream& os) +{ + bench_row<Bench, M, D>(ctx, os); + bench_rec<Bench, M, Ds...>(ctx, os); +} + +/// Run benchmark +template <template <class H, size_t M, size_t D> class Bench> +void +bench(Context& ctx, const std::string& name) +{ + std::ofstream out("hilbert_" + name + ".txt"); + out << "d\tsmall\tstatic\tbounded\tdynamic\n"; + bench_rec<Bench, 8, 2, 4, 8, 16, 32, 64>(ctx, out); +} + +int +main() +{ + Context ctx; + + bench<BenchCoordsToIndex>(ctx, "coords_to_index"); + bench<BenchIndexToCoords>(ctx, "index_to_coords"); + + return 0; +} |