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.
14
15#include "counter.h"
16
17namespace benchmark {
18namespace internal {
19
20double 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;
25 }
26 if (c.flags & Counter::kAvgThreads) {
27 v /= num_threads;
28 }
29 if (c.flags & Counter::kIsIterationInvariant) {
30 v *= iterations;
31 }
32 if (c.flags & Counter::kAvgIterations) {
33 v /= iterations;
34 }
35
36 if (c.flags & Counter::kInvert) { // Invert is *always* last.
37 v = 1.0 / v;
38 }
39 return v;
40}
41
42void 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);
46 }
47}
48
49void 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;
55 }
56 }
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;
62 }
63 }
64}
65
66bool SameNames(UserCounters const& l, UserCounters const& r) {
67 if (&l == &r) return true;
68 if (l.size() != r.size()) {
69 return false;
70 }
71 for (auto const& c : l) {
72 if (r.find(c.first) == r.end()) {
73 return false;
74 }
75 }
76 return true;
77}
78
79} // end namespace internal
80} // end namespace benchmark
81