1 // Copyright 2019 The Abseil Authors.
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 // https://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 <stddef.h>
16
17 #include <string>
18
19 #include "benchmark/benchmark.h"
20 #include "absl/container/fixed_array.h"
21
22 namespace {
23
24 // For benchmarking -- simple class with constructor and destructor that
25 // set an int to a constant..
26 class SimpleClass {
27 public:
SimpleClass()28 SimpleClass() : i(3) {}
~SimpleClass()29 ~SimpleClass() { i = 0; }
30
31 private:
32 int i;
33 };
34
35 template <typename C, size_t stack_size>
BM_FixedArray(benchmark::State & state)36 void BM_FixedArray(benchmark::State& state) {
37 const int size = state.range(0);
38 for (auto _ : state) {
39 absl::FixedArray<C, stack_size> fa(size);
40 benchmark::DoNotOptimize(fa.data());
41 }
42 }
43 BENCHMARK_TEMPLATE(BM_FixedArray, char, absl::kFixedArrayUseDefault)
44 ->Range(0, 1 << 16);
45 BENCHMARK_TEMPLATE(BM_FixedArray, char, 0)->Range(0, 1 << 16);
46 BENCHMARK_TEMPLATE(BM_FixedArray, char, 1)->Range(0, 1 << 16);
47 BENCHMARK_TEMPLATE(BM_FixedArray, char, 16)->Range(0, 1 << 16);
48 BENCHMARK_TEMPLATE(BM_FixedArray, char, 256)->Range(0, 1 << 16);
49 BENCHMARK_TEMPLATE(BM_FixedArray, char, 65536)->Range(0, 1 << 16);
50
51 BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, absl::kFixedArrayUseDefault)
52 ->Range(0, 1 << 16);
53 BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 0)->Range(0, 1 << 16);
54 BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 1)->Range(0, 1 << 16);
55 BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 16)->Range(0, 1 << 16);
56 BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 256)->Range(0, 1 << 16);
57 BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 65536)->Range(0, 1 << 16);
58
59 BENCHMARK_TEMPLATE(BM_FixedArray, std::string, absl::kFixedArrayUseDefault)
60 ->Range(0, 1 << 16);
61 BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 0)->Range(0, 1 << 16);
62 BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 1)->Range(0, 1 << 16);
63 BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 16)->Range(0, 1 << 16);
64 BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 256)->Range(0, 1 << 16);
65 BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 65536)->Range(0, 1 << 16);
66
67 } // namespace
68