xref: /netbsd-src/external/apache2/llvm/dist/libcxx/utils/google-benchmark/src/counter.cc (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1*4d6fc14bSjoerg // Copyright 2015 Google Inc. All rights reserved.
2*4d6fc14bSjoerg //
3*4d6fc14bSjoerg // Licensed under the Apache License, Version 2.0 (the "License");
4*4d6fc14bSjoerg // you may not use this file except in compliance with the License.
5*4d6fc14bSjoerg // You may obtain a copy of the License at
6*4d6fc14bSjoerg //
7*4d6fc14bSjoerg //     http://www.apache.org/licenses/LICENSE-2.0
8*4d6fc14bSjoerg //
9*4d6fc14bSjoerg // Unless required by applicable law or agreed to in writing, software
10*4d6fc14bSjoerg // distributed under the License is distributed on an "AS IS" BASIS,
11*4d6fc14bSjoerg // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*4d6fc14bSjoerg // See the License for the specific language governing permissions and
13*4d6fc14bSjoerg // limitations under the License.
14*4d6fc14bSjoerg 
15*4d6fc14bSjoerg #include "counter.h"
16*4d6fc14bSjoerg 
17*4d6fc14bSjoerg namespace benchmark {
18*4d6fc14bSjoerg namespace internal {
19*4d6fc14bSjoerg 
Finish(Counter const & c,int64_t iterations,double cpu_time,double num_threads)20*4d6fc14bSjoerg double Finish(Counter const& c, int64_t iterations, double cpu_time,
21*4d6fc14bSjoerg               double num_threads) {
22*4d6fc14bSjoerg   double v = c.value;
23*4d6fc14bSjoerg   if (c.flags & Counter::kIsRate) {
24*4d6fc14bSjoerg     v /= cpu_time;
25*4d6fc14bSjoerg   }
26*4d6fc14bSjoerg   if (c.flags & Counter::kAvgThreads) {
27*4d6fc14bSjoerg     v /= num_threads;
28*4d6fc14bSjoerg   }
29*4d6fc14bSjoerg   if (c.flags & Counter::kIsIterationInvariant) {
30*4d6fc14bSjoerg     v *= iterations;
31*4d6fc14bSjoerg   }
32*4d6fc14bSjoerg   if (c.flags & Counter::kAvgIterations) {
33*4d6fc14bSjoerg     v /= iterations;
34*4d6fc14bSjoerg   }
35*4d6fc14bSjoerg   return v;
36*4d6fc14bSjoerg }
37*4d6fc14bSjoerg 
Finish(UserCounters * l,int64_t iterations,double cpu_time,double num_threads)38*4d6fc14bSjoerg void Finish(UserCounters* l, int64_t iterations, double cpu_time, double num_threads) {
39*4d6fc14bSjoerg   for (auto& c : *l) {
40*4d6fc14bSjoerg     c.second.value = Finish(c.second, iterations, cpu_time, num_threads);
41*4d6fc14bSjoerg   }
42*4d6fc14bSjoerg }
43*4d6fc14bSjoerg 
Increment(UserCounters * l,UserCounters const & r)44*4d6fc14bSjoerg void Increment(UserCounters* l, UserCounters const& r) {
45*4d6fc14bSjoerg   // add counters present in both or just in *l
46*4d6fc14bSjoerg   for (auto& c : *l) {
47*4d6fc14bSjoerg     auto it = r.find(c.first);
48*4d6fc14bSjoerg     if (it != r.end()) {
49*4d6fc14bSjoerg       c.second.value = c.second + it->second;
50*4d6fc14bSjoerg     }
51*4d6fc14bSjoerg   }
52*4d6fc14bSjoerg   // add counters present in r, but not in *l
53*4d6fc14bSjoerg   for (auto const& tc : r) {
54*4d6fc14bSjoerg     auto it = l->find(tc.first);
55*4d6fc14bSjoerg     if (it == l->end()) {
56*4d6fc14bSjoerg       (*l)[tc.first] = tc.second;
57*4d6fc14bSjoerg     }
58*4d6fc14bSjoerg   }
59*4d6fc14bSjoerg }
60*4d6fc14bSjoerg 
SameNames(UserCounters const & l,UserCounters const & r)61*4d6fc14bSjoerg bool SameNames(UserCounters const& l, UserCounters const& r) {
62*4d6fc14bSjoerg   if (&l == &r) return true;
63*4d6fc14bSjoerg   if (l.size() != r.size()) {
64*4d6fc14bSjoerg     return false;
65*4d6fc14bSjoerg   }
66*4d6fc14bSjoerg   for (auto const& c : l) {
67*4d6fc14bSjoerg     if (r.find(c.first) == r.end()) {
68*4d6fc14bSjoerg       return false;
69*4d6fc14bSjoerg     }
70*4d6fc14bSjoerg   }
71*4d6fc14bSjoerg   return true;
72*4d6fc14bSjoerg }
73*4d6fc14bSjoerg 
74*4d6fc14bSjoerg }  // end namespace internal
75*4d6fc14bSjoerg }  // end namespace benchmark
76