blob: 3c7a5a723450e7f1e085ceaa91fdb960e7433672 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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
22namespace {
23
24// For benchmarking -- simple class with constructor and destructor that
25// set an int to a constant..
26class SimpleClass {
27 public:
28 SimpleClass() : i(3) {}
29 ~SimpleClass() { i = 0; }
30
31 private:
32 int i;
33};
34
35template <typename C, size_t stack_size>
36void 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}
43BENCHMARK_TEMPLATE(BM_FixedArray, char, absl::kFixedArrayUseDefault)
44 ->Range(0, 1 << 16);
45BENCHMARK_TEMPLATE(BM_FixedArray, char, 0)->Range(0, 1 << 16);
46BENCHMARK_TEMPLATE(BM_FixedArray, char, 1)->Range(0, 1 << 16);
47BENCHMARK_TEMPLATE(BM_FixedArray, char, 16)->Range(0, 1 << 16);
48BENCHMARK_TEMPLATE(BM_FixedArray, char, 256)->Range(0, 1 << 16);
49BENCHMARK_TEMPLATE(BM_FixedArray, char, 65536)->Range(0, 1 << 16);
50
51BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, absl::kFixedArrayUseDefault)
52 ->Range(0, 1 << 16);
53BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 0)->Range(0, 1 << 16);
54BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 1)->Range(0, 1 << 16);
55BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 16)->Range(0, 1 << 16);
56BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 256)->Range(0, 1 << 16);
57BENCHMARK_TEMPLATE(BM_FixedArray, SimpleClass, 65536)->Range(0, 1 << 16);
58
59BENCHMARK_TEMPLATE(BM_FixedArray, std::string, absl::kFixedArrayUseDefault)
60 ->Range(0, 1 << 16);
61BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 0)->Range(0, 1 << 16);
62BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 1)->Range(0, 1 << 16);
63BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 16)->Range(0, 1 << 16);
64BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 256)->Range(0, 1 << 16);
65BENCHMARK_TEMPLATE(BM_FixedArray, std::string, 65536)->Range(0, 1 << 16);
66
67} // namespace