Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / src / counter.cc
blobcf5b78ee3ac6b461d79a2dc61120851164dc9542
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "counter.h"
17 namespace benchmark {
18 namespace internal {
20 double Finish(Counter const& c, IterationCount iterations, double cpu_time,
21 double num_threads) {
22 double v = c.value;
23 if (c.flags & Counter::kIsRate) {
24 v /= cpu_time;
26 if (c.flags & Counter::kAvgThreads) {
27 v /= num_threads;
29 if (c.flags & Counter::kIsIterationInvariant) {
30 v *= iterations;
32 if (c.flags & Counter::kAvgIterations) {
33 v /= iterations;
36 if (c.flags & Counter::kInvert) { // Invert is *always* last.
37 v = 1.0 / v;
39 return v;
42 void Finish(UserCounters* l, IterationCount iterations, double cpu_time,
43 double num_threads) {
44 for (auto& c : *l) {
45 c.second.value = Finish(c.second, iterations, cpu_time, num_threads);
49 void Increment(UserCounters* l, UserCounters const& r) {
50 // add counters present in both or just in *l
51 for (auto& c : *l) {
52 auto it = r.find(c.first);
53 if (it != r.end()) {
54 c.second.value = c.second + it->second;
57 // add counters present in r, but not in *l
58 for (auto const& tc : r) {
59 auto it = l->find(tc.first);
60 if (it == l->end()) {
61 (*l)[tc.first] = tc.second;
66 bool SameNames(UserCounters const& l, UserCounters const& r) {
67 if (&l == &r) return true;
68 if (l.size() != r.size()) {
69 return false;
71 for (auto const& c : l) {
72 if (r.find(c.first) == r.end()) {
73 return false;
76 return true;
79 } // end namespace internal
80 } // end namespace benchmark