1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <cstdlib>
17 #include <ctime>
18 #include <vector>
19 
20 #include "benchmark/benchmark.h"
21 
22 namespace android {
23 namespace os {
24 namespace statsd {
25 
26 namespace {
27 
28 template <typename ContainerType>
benchmarkFunctionForVector(std::vector<ContainerType> & vec,int capacity)29 void benchmarkFunctionForVector(std::vector<ContainerType>& vec, int capacity) {
30     ContainerType result = false;
31     for (int i = 0; i < capacity; i++) {
32         vec[i] = !result;
33         result = !result;
34     }
35 
36     int resultInt = 0;
37     for (int i = 0; i < capacity; i++) {
38         resultInt += vec[i];
39     }
40 
41     // Make sure the variable is not optimized away by compiler
42     benchmark::DoNotOptimize(vec);
43     benchmark::DoNotOptimize(resultInt);
44 }
45 
46 template <typename ContainerType>
benchmarkStdFillForVector(std::vector<ContainerType> & vec,int capacity)47 void benchmarkStdFillForVector(std::vector<ContainerType>& vec, int capacity) {
48     std::fill(vec.begin(), vec.end(), true);
49     int resultInt = 0;
50     for (int i = 0; i < capacity; i++) {
51         resultInt += vec[i];
52     }
53 
54     // Make sure the variable is not optimized away by compiler
55     benchmark::DoNotOptimize(vec);
56     benchmark::DoNotOptimize(resultInt);
57 }
58 
59 }  //  namespace
60 
BM_BasicVectorBoolUsage(benchmark::State & state)61 static void BM_BasicVectorBoolUsage(benchmark::State& state) {
62     const int capacity = state.range(0);
63     std::vector<bool> vec(capacity);
64 
65     while (state.KeepRunning()) {
66         benchmarkFunctionForVector<bool>(vec, capacity);
67     }
68 }
69 BENCHMARK(BM_BasicVectorBoolUsage)->Args({5})->Args({10})->Args({20})->Args({50})->Args({100});
70 
BM_VectorBoolStdFill(benchmark::State & state)71 static void BM_VectorBoolStdFill(benchmark::State& state) {
72     const int capacity = state.range(0);
73     std::vector<bool> vec(capacity);
74 
75     while (state.KeepRunning()) {
76         benchmarkStdFillForVector<bool>(vec, capacity);
77     }
78 }
79 BENCHMARK(BM_VectorBoolStdFill)->Args({5})->Args({10})->Args({20})->Args({50})->Args({100});
80 
BM_BasicVectorInt8Usage(benchmark::State & state)81 static void BM_BasicVectorInt8Usage(benchmark::State& state) {
82     const int capacity = state.range(0);
83     std::vector<int8_t> vec(capacity);
84 
85     while (state.KeepRunning()) {
86         benchmarkFunctionForVector<int8_t>(vec, capacity);
87     }
88 }
89 BENCHMARK(BM_BasicVectorInt8Usage)->Args({5})->Args({10})->Args({20})->Args({50})->Args({100});
90 
BM_VectorInt8StdFill(benchmark::State & state)91 static void BM_VectorInt8StdFill(benchmark::State& state) {
92     const int capacity = state.range(0);
93     std::vector<int8_t> vec(capacity);
94 
95     while (state.KeepRunning()) {
96         benchmarkStdFillForVector<int8_t>(vec, capacity);
97     }
98 }
99 BENCHMARK(BM_VectorInt8StdFill)->Args({5})->Args({10})->Args({20})->Args({50})->Args({100});
100 
101 }  //  namespace statsd
102 }  //  namespace os
103 }  //  namespace android
104