histogram_filling_gsl.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include <benchmark/benchmark.h>
  7. #include <gsl/gsl_histogram.h>
  8. #include <gsl/gsl_histogram2d.h>
  9. #include "../test/throw_exception.hpp"
  10. #include "generator.hpp"
  11. #include <boost/assert.hpp>
  12. struct assert_check {
  13. assert_check() {
  14. BOOST_ASSERT(false); // don't run with asserts enabled
  15. }
  16. } _;
  17. template <class Distribution>
  18. static void fill_1d(benchmark::State& state) {
  19. gsl_histogram* h = gsl_histogram_alloc(100);
  20. gsl_histogram_set_ranges_uniform(h, 0, 1);
  21. generator<Distribution> gen;
  22. for (auto _ : state) benchmark::DoNotOptimize(gsl_histogram_increment(h, gen()));
  23. gsl_histogram_free(h);
  24. state.SetItemsProcessed(state.iterations());
  25. }
  26. template <class Distribution>
  27. static void fill_2d(benchmark::State& state) {
  28. gsl_histogram2d* h = gsl_histogram2d_alloc(100, 100);
  29. gsl_histogram2d_set_ranges_uniform(h, 0, 1, 0, 1);
  30. generator<Distribution> gen;
  31. for (auto _ : state)
  32. benchmark::DoNotOptimize(gsl_histogram2d_increment(h, gen(), gen()));
  33. gsl_histogram2d_free(h);
  34. state.SetItemsProcessed(state.iterations() * 2);
  35. }
  36. BENCHMARK_TEMPLATE(fill_1d, uniform);
  37. BENCHMARK_TEMPLATE(fill_2d, uniform);
  38. BENCHMARK_TEMPLATE(fill_1d, normal);
  39. BENCHMARK_TEMPLATE(fill_2d, normal);