Project
Loading...
Searching...
No Matches
benchmark_Miscellaneous.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#include <benchmark/benchmark.h>
12
13#include <TObjArray.h>
14#include <TH1.h>
15#include <TTree.h>
16#include <TF1.h>
17#include <TRandom.h>
18
19static void BM_dynamic_cast(benchmark::State& state)
20{
21 TCollection* collection = new TList();
22 collection->SetOwner(true);
23 TH1I* h = new TH1I("histo", "histo", 1000, 0, 1000000);
24 collection->Add(h);
25
26 TObject* collectionObj = collection;
27 size_t entries = 0;
28
29 for (auto _ : state) {
30 if (auto* list = dynamic_cast<TList*>(collectionObj)) {
31 entries += list->GetEntries();
32 }
33 }
34
35 (void)entries;
36 delete collection;
37}
38
39static void BM_InheritsFrom(benchmark::State& state)
40{
41 TCollection* collection = new TList();
42 collection->SetOwner(true);
43 TH1I* h = new TH1I("histo", "histo", 1000, 0, 1000000);
44 collection->Add(h);
45
46 TObject* collectionObj = collection;
47 size_t entries = 0;
48 for (auto _ : state) {
49 if (collectionObj->InheritsFrom("TCollection")) {
50 entries += reinterpret_cast<TList*>(collectionObj)->GetEntries();
51 }
52 }
53
54 (void)entries;
55 delete collection;
56}
57
58static void BM_MergeCollectionOnHeap(benchmark::State& state)
59{
60 TCollection* collection = new TObjArray();
61 collection->SetOwner(true);
62 TF1* uni = new TF1("uni", "1", 0, 1000000);
63 TH1I* h = new TH1I("test", "test", 100, 0, 1000000);
64 TH1I* m = new TH1I("merged", "merged", 100, 0, 1000000);
65 h->FillRandom("uni", 50000);
66 collection->Add(h);
67
68 for (auto _ : state) {
69 m->Merge(collection);
70 }
71
72 delete collection;
73 delete uni;
74 delete m;
75}
76
77static void BM_MergeCollectionOnStack(benchmark::State& state)
78{
79 TObjArray collection;
80 collection.SetOwner(true);
81 TF1* uni = new TF1("uni", "1", 0, 1000000);
82 TH1I* h = new TH1I("test", "test", 100, 0, 1000000);
83 TH1I* m = new TH1I("merged", "merged", 100, 0, 1000000);
84 h->FillRandom("uni", 50000);
85 collection.Add(h);
86
87 for (auto _ : state) {
88 m->Merge(&collection);
89 }
90
91 delete uni;
92 delete m;
93}
94
95static void BM_MergeWithoutNoCheck(benchmark::State& state)
96{
97 size_t collectionSize = state.range(0);
98 size_t bins = 62500; // makes 250kB
99
100 TCollection* collection = new TObjArray();
101 collection->SetOwner(true);
102 TF1* uni = new TF1("uni", "1", 0, 1000000);
103 for (size_t i = 0; i < collectionSize; i++) {
104 TH1I* h = new TH1I(("test" + std::to_string(i)).c_str(), "test", bins, 0, 1000000);
105 h->FillRandom("uni", 50000);
106 collection->Add(h);
107 }
108
109 TH1I* m = new TH1I("merged", "merged", bins, 0, 1000000);
110
111 for (auto _ : state) {
112 m->Merge(collection);
113 }
114
115 delete collection;
116 delete m;
117 delete uni;
118}
119
120static void BM_MergeWithNoCheck(benchmark::State& state)
121{
122 size_t collectionSize = state.range(0);
123 size_t bins = 62500; // makes 250kB
124
125 TCollection* collection = new TObjArray();
126 collection->SetOwner(true);
127 TF1* uni = new TF1("uni", "1", 0, 1000000);
128 for (size_t i = 0; i < collectionSize; i++) {
129 TH1I* h = new TH1I(("test" + std::to_string(i)).c_str(), "test", bins, 0, 1000000);
130 h->FillRandom("uni", 50000);
131 collection->Add(h);
132 }
133
134 TH1I* m = new TH1I("merged", "merged", bins, 0, 1000000);
135
136 for (auto _ : state) {
137 m->Merge(collection);
138 }
139
140 delete collection;
141 delete m;
142 delete uni;
143}
144
145static void BM_ClassComparisonCString(benchmark::State& state)
146{
147 bool thisTriesToAvoidOptimization = std::rand();
148 TH1* h;
149 if (thisTriesToAvoidOptimization) {
150 h = new TH1I("histo", "histo", 1000, 0, 1000000);
151 } else {
152 h = new TH1F("histo", "histo", 1000, 0, 1000000);
153 }
154
155 size_t entries = 0;
156
157 for (auto _ : state) {
158 const char* className = h->ClassName();
159 if (strncmp(className, "TH1", 3) == 0) {
160 entries += 1;
161 }
162 }
163
164 (void)entries;
165 delete h;
166}
167
168static void BM_ClassComparisonInterface(benchmark::State& state)
169{
170 bool thisTriesToAvoidOptimization = std::rand();
171 TH1* h;
172 if (thisTriesToAvoidOptimization) {
173 h = new TH1I("histo", "histo", 1000, 0, 1000000);
174 } else {
175 h = new TH1F("histo", "histo", 1000, 0, 1000000);
176 }
177
178 size_t entries = 0;
179
180 // not sure if compiler won't be to clever about this, making it static...
181 for (auto _ : state) {
182 if (h->InheritsFrom(TH1::Class())) {
183 entries += 1;
184 }
185 }
186
187 (void)entries;
188 delete h;
189}
190
191// TEST: See which way of casting ROOT objects is faster.
192// RESULT: dynamic_cast wins by an order of magnitude.
193BENCHMARK(BM_dynamic_cast);
194BENCHMARK(BM_InheritsFrom);
195// TEST: See if there is a difference between merging TH1 (on heap) stored in a collection on stack vs on heap.
196// RESULT: The difference is negligible.
197BENCHMARK(BM_MergeCollectionOnHeap);
198BENCHMARK(BM_MergeCollectionOnStack);
199// TEST: see if -NOCHECK indeed improves the performance
200// RESULT: Probably barely
201BENCHMARK(BM_MergeWithoutNoCheck)->Arg(1)->Arg(100);
202BENCHMARK(BM_MergeWithNoCheck)->Arg(1)->Arg(100);
203// TEST: see if which way of checking the parent class is faster
204// RESULT: inherits from is 1.5x slower
205BENCHMARK(BM_ClassComparisonCString);
206BENCHMARK(BM_ClassComparisonInterface);
207
benchmark::State & state
const auto bins
Definition PID.cxx:49
int32_t i
const size_t collectionSize
BENCHMARK_MAIN()
BENCHMARK(BM_dynamic_cast)
Class for time synchronization of RawReader instances.
const GLfloat * m
Definition glcorearb.h:4066
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
Definition list.h:40