axis_index.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include <benchmark/benchmark.h>
  7. #include <boost/histogram/axis.hpp>
  8. #include <numeric>
  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. using namespace boost::histogram;
  18. template <class Distribution>
  19. static void regular(benchmark::State& state) {
  20. auto a = axis::regular<>(100, 0.0, 1.0);
  21. generator<Distribution> gen;
  22. for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
  23. }
  24. template <class Distribution>
  25. static void circular(benchmark::State& state) {
  26. auto a = axis::circular<>(100, 0.0, 1.0);
  27. generator<Distribution> gen;
  28. for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
  29. }
  30. template <class T, class Distribution>
  31. static void integer(benchmark::State& state) {
  32. auto a = axis::integer<T>(0, 1);
  33. generator<Distribution> gen;
  34. for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
  35. }
  36. template <class Distribution>
  37. static void variable(benchmark::State& state) {
  38. std::vector<double> v;
  39. for (double x = 0; x <= state.range(0); ++x) { v.push_back(x / state.range(0)); }
  40. auto a = axis::variable<>(v);
  41. generator<Distribution> gen;
  42. for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
  43. }
  44. static void category(benchmark::State& state) {
  45. std::vector<int> v(state.range(0));
  46. std::iota(v.begin(), v.end(), 0);
  47. auto a = axis::category<int>(v);
  48. generator<uniform_int> gen(static_cast<int>(state.range(0)));
  49. for (auto _ : state) benchmark::DoNotOptimize(a.index(gen()));
  50. }
  51. BENCHMARK_TEMPLATE(regular, uniform);
  52. BENCHMARK_TEMPLATE(regular, normal);
  53. BENCHMARK_TEMPLATE(circular, uniform);
  54. BENCHMARK_TEMPLATE(circular, normal);
  55. BENCHMARK_TEMPLATE(integer, int, uniform);
  56. BENCHMARK_TEMPLATE(integer, int, normal);
  57. BENCHMARK_TEMPLATE(integer, double, uniform);
  58. BENCHMARK_TEMPLATE(integer, double, normal);
  59. BENCHMARK_TEMPLATE(variable, uniform)->RangeMultiplier(10)->Range(10, 10000);
  60. BENCHMARK_TEMPLATE(variable, normal)->RangeMultiplier(10)->Range(10, 10000);
  61. BENCHMARK(category)->RangeMultiplier(10)->Range(10, 10000);