1 // Copyright 2015-2019 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 <gsl/gsl_histogram.h>
9 #include <gsl/gsl_histogram2d.h>
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 template <class Distribution>
fill_1d(benchmark::State & state)21 static void fill_1d(benchmark::State& state) {
22   gsl_histogram* h = gsl_histogram_alloc(100);
23   gsl_histogram_set_ranges_uniform(h, 0, 1);
24   generator<Distribution> gen;
25   for (auto _ : state) benchmark::DoNotOptimize(gsl_histogram_increment(h, gen()));
26   gsl_histogram_free(h);
27   state.SetItemsProcessed(state.iterations());
28 }
29 
30 template <class Distribution>
fill_2d(benchmark::State & state)31 static void fill_2d(benchmark::State& state) {
32   gsl_histogram2d* h = gsl_histogram2d_alloc(100, 100);
33   gsl_histogram2d_set_ranges_uniform(h, 0, 1, 0, 1);
34   generator<Distribution> gen;
35   for (auto _ : state)
36     benchmark::DoNotOptimize(gsl_histogram2d_increment(h, gen(), gen()));
37   gsl_histogram2d_free(h);
38   state.SetItemsProcessed(state.iterations() * 2);
39 }
40 
41 BENCHMARK_TEMPLATE(fill_1d, uniform);
42 BENCHMARK_TEMPLATE(fill_2d, uniform);
43 
44 BENCHMARK_TEMPLATE(fill_1d, normal);
45 BENCHMARK_TEMPLATE(fill_2d, normal);
46