equal_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <cstdint>
00016 #include <cstring>
00017 
00018 #include "benchmark/benchmark.h"
00019 #include "absl/algorithm/algorithm.h"
00020 
00021 namespace {
00022 
00023 // The range of sequence sizes to benchmark.
00024 constexpr int kMinBenchmarkSize = 1024;
00025 constexpr int kMaxBenchmarkSize = 8 * 1024 * 1024;
00026 
00027 // A user-defined type for use in equality benchmarks. Note that we expect
00028 // std::memcmp to win for this type: libstdc++'s std::equal only defers to
00029 // memcmp for integral types. This is because it is not straightforward to
00030 // guarantee that std::memcmp would produce a result "as-if" compared by
00031 // operator== for other types (example gotchas: NaN floats, structs with
00032 // padding).
00033 struct EightBits {
00034   explicit EightBits(int /* unused */) : data(0) {}
00035   bool operator==(const EightBits& rhs) const { return data == rhs.data; }
00036   uint8_t data;
00037 };
00038 
00039 template <typename T>
00040 void BM_absl_equal_benchmark(benchmark::State& state) {
00041   std::vector<T> xs(state.range(0), T(0));
00042   std::vector<T> ys = xs;
00043   while (state.KeepRunning()) {
00044     const bool same = absl::equal(xs.begin(), xs.end(), ys.begin(), ys.end());
00045     benchmark::DoNotOptimize(same);
00046   }
00047 }
00048 
00049 template <typename T>
00050 void BM_std_equal_benchmark(benchmark::State& state) {
00051   std::vector<T> xs(state.range(0), T(0));
00052   std::vector<T> ys = xs;
00053   while (state.KeepRunning()) {
00054     const bool same = std::equal(xs.begin(), xs.end(), ys.begin());
00055     benchmark::DoNotOptimize(same);
00056   }
00057 }
00058 
00059 template <typename T>
00060 void BM_memcmp_benchmark(benchmark::State& state) {
00061   std::vector<T> xs(state.range(0), T(0));
00062   std::vector<T> ys = xs;
00063   while (state.KeepRunning()) {
00064     const bool same =
00065         std::memcmp(xs.data(), ys.data(), xs.size() * sizeof(T)) == 0;
00066     benchmark::DoNotOptimize(same);
00067   }
00068 }
00069 
00070 // The expectation is that the compiler should be able to elide the equality
00071 // comparison altogether for sufficiently simple types.
00072 template <typename T>
00073 void BM_absl_equal_self_benchmark(benchmark::State& state) {
00074   std::vector<T> xs(state.range(0), T(0));
00075   while (state.KeepRunning()) {
00076     const bool same = absl::equal(xs.begin(), xs.end(), xs.begin(), xs.end());
00077     benchmark::DoNotOptimize(same);
00078   }
00079 }
00080 
00081 BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint8_t)
00082     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00083 BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint8_t)
00084     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00085 BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint8_t)
00086     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00087 BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint8_t)
00088     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00089 
00090 BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint16_t)
00091     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00092 BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint16_t)
00093     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00094 BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint16_t)
00095     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00096 BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint16_t)
00097     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00098 
00099 BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint32_t)
00100     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00101 BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint32_t)
00102     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00103 BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint32_t)
00104     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00105 BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint32_t)
00106     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00107 
00108 BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint64_t)
00109     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00110 BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint64_t)
00111     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00112 BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint64_t)
00113     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00114 BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint64_t)
00115     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00116 
00117 BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, EightBits)
00118     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00119 BENCHMARK_TEMPLATE(BM_std_equal_benchmark, EightBits)
00120     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00121 BENCHMARK_TEMPLATE(BM_memcmp_benchmark, EightBits)
00122     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00123 BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, EightBits)
00124     ->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
00125 
00126 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14