histogram_filling_root.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <TH1I.h>
  7. #include <TH2I.h>
  8. #include <TH3I.h>
  9. #include <THn.h>
  10. #include <benchmark/benchmark.h>
  11. #include "generator.hpp"
  12. #include <boost/assert.hpp>
  13. struct assert_check {
  14. assert_check() {
  15. BOOST_ASSERT(false); // don't run with asserts enabled
  16. }
  17. } _;
  18. template <class Distribution>
  19. static void fill_1d(benchmark::State& state) {
  20. TH1I h("", "", 100, 0, 1);
  21. generator<Distribution> gen;
  22. for (auto _ : state) benchmark::DoNotOptimize(h.Fill(gen()));
  23. state.SetItemsProcessed(state.iterations());
  24. }
  25. template <class Distribution>
  26. static void fill_2d(benchmark::State& state) {
  27. TH2I h("", "", 100, 0, 1, 100, 0, 1);
  28. generator<Distribution> gen;
  29. for (auto _ : state) benchmark::DoNotOptimize(h.Fill(gen(), gen()));
  30. state.SetItemsProcessed(state.iterations() * 2);
  31. }
  32. template <class Distribution>
  33. static void fill_3d(benchmark::State& state) {
  34. TH3I h("", "", 100, 0, 1, 100, 0, 1, 100, 0, 1);
  35. generator<Distribution> gen;
  36. for (auto _ : state) benchmark::DoNotOptimize(h.Fill(gen(), gen(), gen()));
  37. state.SetItemsProcessed(state.iterations() * 3);
  38. }
  39. template <class Distribution>
  40. static void fill_6d(benchmark::State& state) {
  41. int bin[] = {10, 10, 10, 10, 10, 10};
  42. double min[] = {0, 0, 0, 0, 0, 0};
  43. double max[] = {1, 1, 1, 1, 1, 1};
  44. THnI h("", "", 6, bin, min, max);
  45. generator<Distribution> gen;
  46. for (auto _ : state) {
  47. const double buf[6] = {gen(), gen(), gen(), gen(), gen(), gen()};
  48. benchmark::DoNotOptimize(h.Fill(buf));
  49. }
  50. state.SetItemsProcessed(state.iterations() * 6);
  51. }
  52. BENCHMARK_TEMPLATE(fill_1d, uniform);
  53. BENCHMARK_TEMPLATE(fill_2d, uniform);
  54. BENCHMARK_TEMPLATE(fill_3d, uniform);
  55. BENCHMARK_TEMPLATE(fill_6d, uniform);
  56. BENCHMARK_TEMPLATE(fill_1d, normal);
  57. BENCHMARK_TEMPLATE(fill_2d, normal);
  58. BENCHMARK_TEMPLATE(fill_3d, normal);
  59. BENCHMARK_TEMPLATE(fill_6d, normal);