blob: aa14cd8092f94efebf20951b9e20cc31a53704db [file] [log] [blame]
Eric Fiselier133a7202017-04-18 07:17:201// 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
Louis Dionne5208ec52021-07-12 17:15:3420double Finish(Counter const& c, IterationCount iterations, double cpu_time,
Eric Fiselierfcafd3e2018-07-10 04:02:0021 double num_threads) {
Eric Fiselier133a7202017-04-18 07:17:2022 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 }
Eric Fiselierfcafd3e2018-07-10 04:02:0029 if (c.flags & Counter::kIsIterationInvariant) {
Mircea Trofina5b79712024-03-04 22:11:3030 v *= static_cast<double>(iterations);
Eric Fiselierfcafd3e2018-07-10 04:02:0031 }
32 if (c.flags & Counter::kAvgIterations) {
Mircea Trofina5b79712024-03-04 22:11:3033 v /= static_cast<double>(iterations);
Eric Fiselierfcafd3e2018-07-10 04:02:0034 }
Louis Dionne5208ec52021-07-12 17:15:3435
36 if (c.flags & Counter::kInvert) { // Invert is *always* last.
37 v = 1.0 / v;
38 }
Eric Fiselier133a7202017-04-18 07:17:2039 return v;
40}
41
Louis Dionne5208ec52021-07-12 17:15:3442void Finish(UserCounters* l, IterationCount iterations, double cpu_time,
43 double num_threads) {
Eric Fiselierfcafd3e2018-07-10 04:02:0044 for (auto& c : *l) {
45 c.second.value = Finish(c.second, iterations, cpu_time, num_threads);
Eric Fiselier133a7202017-04-18 07:17:2046 }
47}
48
Eric Fiselierfcafd3e2018-07-10 04:02:0049void Increment(UserCounters* l, UserCounters const& r) {
Eric Fiselier133a7202017-04-18 07:17:2050 // add counters present in both or just in *l
Eric Fiselierfcafd3e2018-07-10 04:02:0051 for (auto& c : *l) {
Eric Fiselier133a7202017-04-18 07:17:2052 auto it = r.find(c.first);
53 if (it != r.end()) {
Eric Fiselier19039762018-01-18 04:23:0154 c.second.value = c.second + it->second;
Eric Fiselier133a7202017-04-18 07:17:2055 }
56 }
57 // add counters present in r, but not in *l
Eric Fiselierfcafd3e2018-07-10 04:02:0058 for (auto const& tc : r) {
Eric Fiselier133a7202017-04-18 07:17:2059 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) {
Eric Fiselier19039762018-01-18 04:23:0172 if (r.find(c.first) == r.end()) {
Eric Fiselier133a7202017-04-18 07:17:2073 return false;
74 }
75 }
76 return true;
77}
78
Eric Fiselierfcafd3e2018-07-10 04:02:0079} // end namespace internal
80} // end namespace benchmark