equal_benchmark.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2017 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 <cstdint>
  15. #include <cstring>
  16. #include "benchmark/benchmark.h"
  17. #include "absl/algorithm/algorithm.h"
  18. namespace {
  19. // The range of sequence sizes to benchmark.
  20. constexpr int kMinBenchmarkSize = 1024;
  21. constexpr int kMaxBenchmarkSize = 8 * 1024 * 1024;
  22. // A user-defined type for use in equality benchmarks. Note that we expect
  23. // std::memcmp to win for this type: libstdc++'s std::equal only defers to
  24. // memcmp for integral types. This is because it is not straightforward to
  25. // guarantee that std::memcmp would produce a result "as-if" compared by
  26. // operator== for other types (example gotchas: NaN floats, structs with
  27. // padding).
  28. struct EightBits {
  29. explicit EightBits(int /* unused */) : data(0) {}
  30. bool operator==(const EightBits& rhs) const { return data == rhs.data; }
  31. uint8_t data;
  32. };
  33. template <typename T>
  34. void BM_absl_equal_benchmark(benchmark::State& state) {
  35. std::vector<T> xs(state.range(0), T(0));
  36. std::vector<T> ys = xs;
  37. while (state.KeepRunning()) {
  38. const bool same = absl::equal(xs.begin(), xs.end(), ys.begin(), ys.end());
  39. benchmark::DoNotOptimize(same);
  40. }
  41. }
  42. template <typename T>
  43. void BM_std_equal_benchmark(benchmark::State& state) {
  44. std::vector<T> xs(state.range(0), T(0));
  45. std::vector<T> ys = xs;
  46. while (state.KeepRunning()) {
  47. const bool same = std::equal(xs.begin(), xs.end(), ys.begin());
  48. benchmark::DoNotOptimize(same);
  49. }
  50. }
  51. template <typename T>
  52. void BM_memcmp_benchmark(benchmark::State& state) {
  53. std::vector<T> xs(state.range(0), T(0));
  54. std::vector<T> ys = xs;
  55. while (state.KeepRunning()) {
  56. const bool same =
  57. std::memcmp(xs.data(), ys.data(), xs.size() * sizeof(T)) == 0;
  58. benchmark::DoNotOptimize(same);
  59. }
  60. }
  61. // The expectation is that the compiler should be able to elide the equality
  62. // comparison altogether for sufficiently simple types.
  63. template <typename T>
  64. void BM_absl_equal_self_benchmark(benchmark::State& state) {
  65. std::vector<T> xs(state.range(0), T(0));
  66. while (state.KeepRunning()) {
  67. const bool same = absl::equal(xs.begin(), xs.end(), xs.begin(), xs.end());
  68. benchmark::DoNotOptimize(same);
  69. }
  70. }
  71. BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint8_t)
  72. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  73. BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint8_t)
  74. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  75. BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint8_t)
  76. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  77. BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint8_t)
  78. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  79. BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint16_t)
  80. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  81. BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint16_t)
  82. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  83. BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint16_t)
  84. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  85. BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint16_t)
  86. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  87. BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint32_t)
  88. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  89. BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint32_t)
  90. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  91. BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint32_t)
  92. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  93. BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint32_t)
  94. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  95. BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint64_t)
  96. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  97. BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint64_t)
  98. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  99. BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint64_t)
  100. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  101. BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint64_t)
  102. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  103. BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, EightBits)
  104. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  105. BENCHMARK_TEMPLATE(BM_std_equal_benchmark, EightBits)
  106. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  107. BENCHMARK_TEMPLATE(BM_memcmp_benchmark, EightBits)
  108. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  109. BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, EightBits)
  110. ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
  111. } // namespace