histogram_filling.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <boost/histogram/axis/regular.hpp>
  8. #include <boost/histogram/storage_adaptor.hpp>
  9. #include <memory>
  10. #include "../test/throw_exception.hpp"
  11. #include "../test/utility_histogram.hpp"
  12. #include "generator.hpp"
  13. #include <boost/assert.hpp>
  14. struct assert_check {
  15. assert_check() {
  16. BOOST_ASSERT(false); // don't run with asserts enabled
  17. }
  18. } _;
  19. using SStore = std::vector<double>;
  20. // make benchmark compatible with older versions of the library
  21. #if __has_include(<boost/histogram/unlimited_storage.hpp>)
  22. #include <boost/histogram/unlimited_storage.hpp>
  23. using DStore = boost::histogram::unlimited_storage<>;
  24. #else
  25. #include <boost/histogram/adaptive_storage.hpp>
  26. using DStore = boost::histogram::adaptive_storage<>;
  27. #endif
  28. using namespace boost::histogram;
  29. using reg = axis::regular<>;
  30. template <class Distribution, class Tag, class Storage = SStore>
  31. static void fill_1d(benchmark::State& state) {
  32. auto h = make_s(Tag(), Storage(), reg(100, 0, 1));
  33. auto gen = generator<Distribution>();
  34. for (auto _ : state) benchmark::DoNotOptimize(h(gen()));
  35. state.SetItemsProcessed(state.iterations());
  36. }
  37. template <class Distribution, class Tag, class Storage = SStore>
  38. static void fill_n_1d(benchmark::State& state) {
  39. auto h = make_s(Tag(), Storage(), reg(100, 0, 1));
  40. auto gen = generator<Distribution>();
  41. for (auto _ : state) h.fill(gen);
  42. state.SetItemsProcessed(state.iterations() * gen.size());
  43. }
  44. template <class Distribution, class Tag, class Storage = SStore>
  45. static void fill_2d(benchmark::State& state) {
  46. auto h = make_s(Tag(), Storage(), reg(100, 0, 1), reg(100, 0, 1));
  47. auto gen = generator<Distribution>();
  48. for (auto _ : state) benchmark::DoNotOptimize(h(gen(), gen()));
  49. state.SetItemsProcessed(state.iterations() * 2);
  50. }
  51. template <class Distribution, class Tag, class Storage = SStore>
  52. static void fill_n_2d(benchmark::State& state) {
  53. auto h = make_s(Tag(), Storage(), reg(100, 0, 1), reg(100, 0, 1));
  54. auto gen = generator<Distribution>();
  55. auto v = {gen, gen};
  56. for (auto _ : state) h.fill(v);
  57. state.SetItemsProcessed(state.iterations() * 2 * gen.size());
  58. }
  59. template <class Distribution, class Tag, class Storage = SStore>
  60. static void fill_3d(benchmark::State& state) {
  61. auto h = make_s(Tag(), Storage(), reg(100, 0, 1), reg(100, 0, 1), reg(100, 0, 1));
  62. auto gen = generator<Distribution>();
  63. for (auto _ : state) benchmark::DoNotOptimize(h(gen(), gen(), gen()));
  64. state.SetItemsProcessed(state.iterations() * 3);
  65. }
  66. template <class Distribution, class Tag, class Storage = SStore>
  67. static void fill_n_3d(benchmark::State& state) {
  68. auto h = make_s(Tag(), Storage(), reg(100, 0, 1), reg(100, 0, 1), reg(100, 0, 1));
  69. auto gen = generator<Distribution>();
  70. auto v = {gen, gen, gen};
  71. for (auto _ : state) h.fill(v);
  72. state.SetItemsProcessed(state.iterations() * 3 * gen.size());
  73. }
  74. template <class Distribution, class Tag, class Storage = SStore>
  75. static void fill_6d(benchmark::State& state) {
  76. auto h = make_s(Tag(), Storage(), reg(10, 0, 1), reg(10, 0, 1), reg(10, 0, 1),
  77. reg(10, 0, 1), reg(10, 0, 1), reg(10, 0, 1));
  78. auto gen = generator<Distribution>();
  79. for (auto _ : state)
  80. benchmark::DoNotOptimize(h(gen(), gen(), gen(), gen(), gen(), gen()));
  81. state.SetItemsProcessed(state.iterations() * 6);
  82. }
  83. template <class Distribution, class Tag, class Storage = SStore>
  84. static void fill_n_6d(benchmark::State& state) {
  85. auto h = make_s(Tag(), Storage(), reg(10, 0, 1), reg(10, 0, 1), reg(10, 0, 1),
  86. reg(10, 0, 1), reg(10, 0, 1), reg(10, 0, 1));
  87. auto gen = generator<Distribution>();
  88. auto v = {gen, gen, gen, gen, gen, gen};
  89. for (auto _ : state) h.fill(v);
  90. state.SetItemsProcessed(state.iterations() * 6 * gen.size());
  91. }
  92. BENCHMARK_TEMPLATE(fill_1d, uniform, static_tag);
  93. // BENCHMARK_TEMPLATE(fill_1d, uniform, static_tag, DStore);
  94. BENCHMARK_TEMPLATE(fill_1d, normal, static_tag);
  95. // BENCHMARK_TEMPLATE(fill_1d, normal, static_tag, DStore);
  96. BENCHMARK_TEMPLATE(fill_1d, uniform, dynamic_tag);
  97. // BENCHMARK_TEMPLATE(fill_1d, uniform, dynamic_tag, DStore);
  98. BENCHMARK_TEMPLATE(fill_1d, normal, dynamic_tag);
  99. // BENCHMARK_TEMPLATE(fill_1d, normal, dynamic_tag, DStore);
  100. BENCHMARK_TEMPLATE(fill_n_1d, uniform, static_tag);
  101. // BENCHMARK_TEMPLATE(fill_n_1d, uniform, static_tag, DStore);
  102. BENCHMARK_TEMPLATE(fill_n_1d, normal, static_tag);
  103. // BENCHMARK_TEMPLATE(fill_n_1d, normal, static_tag, DStore);
  104. BENCHMARK_TEMPLATE(fill_n_1d, uniform, dynamic_tag);
  105. // BENCHMARK_TEMPLATE(fill_n_1d, uniform, dynamic_tag, DStore);
  106. BENCHMARK_TEMPLATE(fill_n_1d, normal, dynamic_tag);
  107. // BENCHMARK_TEMPLATE(fill_n_1d, normal, dynamic_tag, DStore);
  108. BENCHMARK_TEMPLATE(fill_2d, uniform, static_tag);
  109. // BENCHMARK_TEMPLATE(fill_2d, uniform, static_tag, DStore);
  110. BENCHMARK_TEMPLATE(fill_2d, normal, static_tag);
  111. // BENCHMARK_TEMPLATE(fill_2d, normal, static_tag, DStore);
  112. BENCHMARK_TEMPLATE(fill_2d, uniform, dynamic_tag);
  113. // BENCHMARK_TEMPLATE(fill_2d, uniform, dynamic_tag, DStore);
  114. BENCHMARK_TEMPLATE(fill_2d, normal, dynamic_tag);
  115. // BENCHMARK_TEMPLATE(fill_2d, normal, dynamic_tag, DStore);
  116. BENCHMARK_TEMPLATE(fill_n_2d, uniform, static_tag);
  117. // BENCHMARK_TEMPLATE(fill_n_2d, uniform, static_tag, DStore);
  118. BENCHMARK_TEMPLATE(fill_n_2d, normal, static_tag);
  119. // BENCHMARK_TEMPLATE(fill_n_2d, normal, static_tag, DStore);
  120. BENCHMARK_TEMPLATE(fill_n_2d, uniform, dynamic_tag);
  121. // BENCHMARK_TEMPLATE(fill_n_2d, uniform, dynamic_tag, DStore);
  122. BENCHMARK_TEMPLATE(fill_n_2d, normal, dynamic_tag);
  123. // BENCHMARK_TEMPLATE(fill_n_2d, normal, dynamic_tag, DStore);
  124. BENCHMARK_TEMPLATE(fill_3d, uniform, static_tag);
  125. // BENCHMARK_TEMPLATE(fill_3d, uniform, static_tag, DStore);
  126. BENCHMARK_TEMPLATE(fill_3d, normal, static_tag);
  127. // BENCHMARK_TEMPLATE(fill_3d, normal, static_tag, DStore);
  128. BENCHMARK_TEMPLATE(fill_3d, uniform, dynamic_tag);
  129. // BENCHMARK_TEMPLATE(fill_3d, uniform, dynamic_tag, DStore);
  130. BENCHMARK_TEMPLATE(fill_3d, normal, dynamic_tag);
  131. // BENCHMARK_TEMPLATE(fill_3d, normal, dynamic_tag, DStore);
  132. BENCHMARK_TEMPLATE(fill_n_3d, uniform, static_tag);
  133. // BENCHMARK_TEMPLATE(fill_n_3d, uniform, static_tag, DStore);
  134. BENCHMARK_TEMPLATE(fill_n_3d, normal, static_tag);
  135. // BENCHMARK_TEMPLATE(fill_n_3d, normal, static_tag, DStore);
  136. BENCHMARK_TEMPLATE(fill_n_3d, uniform, dynamic_tag);
  137. // BENCHMARK_TEMPLATE(fill_n_3d, uniform, dynamic_tag, DStore);
  138. BENCHMARK_TEMPLATE(fill_n_3d, normal, dynamic_tag);
  139. // BENCHMARK_TEMPLATE(fill_n_3d, normal, dynamic_tag, DStore);
  140. BENCHMARK_TEMPLATE(fill_6d, uniform, static_tag);
  141. // BENCHMARK_TEMPLATE(fill_6d, uniform, static_tag, DStore);
  142. BENCHMARK_TEMPLATE(fill_6d, normal, static_tag);
  143. // BENCHMARK_TEMPLATE(fill_6d, normal, static_tag, DStore);
  144. BENCHMARK_TEMPLATE(fill_6d, uniform, dynamic_tag);
  145. // BENCHMARK_TEMPLATE(fill_6d, uniform, dynamic_tag, DStore);
  146. BENCHMARK_TEMPLATE(fill_6d, normal, dynamic_tag);
  147. // BENCHMARK_TEMPLATE(fill_6d, normal, dynamic_tag, DStore);
  148. BENCHMARK_TEMPLATE(fill_n_6d, uniform, static_tag);
  149. // BENCHMARK_TEMPLATE(fill_n_6d, uniform, static_tag, DStore);
  150. BENCHMARK_TEMPLATE(fill_n_6d, normal, static_tag);
  151. // BENCHMARK_TEMPLATE(fill_n_6d, normal, static_tag, DStore);
  152. BENCHMARK_TEMPLATE(fill_n_6d, uniform, dynamic_tag);
  153. // BENCHMARK_TEMPLATE(fill_n_6d, uniform, dynamic_tag, DStore);
  154. BENCHMARK_TEMPLATE(fill_n_6d, normal, dynamic_tag);
  155. // BENCHMARK_TEMPLATE(fill_n_6d, normal, dynamic_tag, DStore);