fixed_array_benchmark.cc 2.5 KB

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