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 return v;
36}
37
38void Finish(UserCounters* l, IterationCount iterations, double cpu_time,
39 double num_threads) {
40 for (auto& c : *l) {
41 c.second.value = Finish(c.second, iterations, cpu_time, num_threads);
42 }
43}
44
45void Increment(UserCounters* l, UserCounters const& r) {
46 // add counters present in both or just in *l
47 for (auto& c : *l) {
48 auto it = r.find(c.first);
49 if (it != r.end()) {
50 c.second.value = c.second + it->second;
51 }
52 }
53 // add counters present in r, but not in *l
54 for (auto const& tc : r) {
55 auto it = l->find(tc.first);
56 if (it == l->end()) {
57 (*l)[tc.first] = tc.second;
58 }
59 }
60}
61
62bool SameNames(UserCounters const& l, UserCounters const& r) {
63 if (&l == &r) return true;
64 if (l.size() != r.size()) {
65 return false;
66 }
67 for (auto const& c : l) {
68 if (r.find(c.first) == r.end()) {
69 return false;
70 }
71 }
72 return true;
73}
74
75} // end namespace internal
76} // end namespace benchmark
77