00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <cstdint>
00016 #include <cstring>
00017
00018 #include "benchmark/benchmark.h"
00019 #include "absl/algorithm/algorithm.h"
00020
00021 namespace {
00022
00023
00024 constexpr int kMinBenchmarkSize = 1024;
00025 constexpr int kMaxBenchmarkSize = 8 * 1024 * 1024;
00026
00027
00028
00029
00030
00031
00032
00033 struct EightBits {
00034 explicit EightBits(int ) : 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
00071
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 }