• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <benchmark/benchmark.h>
8 #include <boost/histogram/axis.hpp>
9 #include <numeric>
10 #include "../test/throw_exception.hpp"
11 #include "generator.hpp"
12 
13 #include <cassert>
14 struct assert_check {
assert_checkassert_check15   assert_check() {
16     assert(false); // don't run with asserts enabled
17   }
18 } _;
19 
20 using namespace boost::histogram;
21 
22 template <class Distribution>
regular(benchmark::State & state)23 static void regular(benchmark::State& state) {
24   auto a = axis::regular<>(100, 0.0, 1.0);
25   generator<Distribution> gen;
26   for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
27 }
28 
29 template <class Distribution>
circular(benchmark::State & state)30 static void circular(benchmark::State& state) {
31   auto a = axis::circular<>(100, 0.0, 1.0);
32   generator<Distribution> gen;
33   for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
34 }
35 
36 template <class T, class Distribution>
integer(benchmark::State & state)37 static void integer(benchmark::State& state) {
38   auto a = axis::integer<T>(0, 1);
39   generator<Distribution> gen;
40   for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
41 }
42 
43 template <class Distribution>
variable(benchmark::State & state)44 static void variable(benchmark::State& state) {
45   std::vector<double> v;
46   for (double x = 0; x <= state.range(0); ++x) { v.push_back(x / state.range(0)); }
47   auto a = axis::variable<>(v);
48   generator<Distribution> gen;
49   for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
50 }
51 
category(benchmark::State & state)52 static void category(benchmark::State& state) {
53   std::vector<int> v(state.range(0));
54   std::iota(v.begin(), v.end(), 0);
55   auto a = axis::category<int>(v);
56   generator<uniform_int> gen(static_cast<int>(state.range(0)));
57   for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
58 }
59 
boolean(benchmark::State & state)60 static void boolean(benchmark::State& state) {
61   auto a = axis::boolean<>();
62   generator<uniform_int> gen(1);
63   for (auto _ : state) benchmark::DoNotOptimize(a.index(static_cast<bool>(gen())));
64 }
65 
66 BENCHMARK_TEMPLATE(regular, uniform);
67 BENCHMARK_TEMPLATE(regular, normal);
68 BENCHMARK_TEMPLATE(circular, uniform);
69 BENCHMARK_TEMPLATE(circular, normal);
70 BENCHMARK_TEMPLATE(integer, int, uniform);
71 BENCHMARK_TEMPLATE(integer, int, normal);
72 BENCHMARK_TEMPLATE(integer, double, uniform);
73 BENCHMARK_TEMPLATE(integer, double, normal);
74 BENCHMARK_TEMPLATE(variable, uniform)->RangeMultiplier(10)->Range(10, 10000);
75 BENCHMARK_TEMPLATE(variable, normal)->RangeMultiplier(10)->Range(10, 10000);
76 BENCHMARK(category)->RangeMultiplier(10)->Range(10, 10000);
77 BENCHMARK(boolean);
78