|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <benchmark/benchmark.h> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "test_iterators.h" |
| 14 | + |
| 15 | +static void bm_starts_with_contiguous_iter_with_memcmp_optimization(benchmark::State& state) { |
| 16 | + std::vector<int> a(state.range(), 1); |
| 17 | + std::vector<int> p(state.range(), 1); |
| 18 | + |
| 19 | + for (auto _ : state) { |
| 20 | + benchmark::DoNotOptimize(a); |
| 21 | + benchmark::DoNotOptimize(p); |
| 22 | + |
| 23 | + auto begin1 = contiguous_iterator(a.data()); |
| 24 | + auto end1 = contiguous_iterator(a.data() + a.size()); |
| 25 | + auto begin2 = contiguous_iterator(p.data()); |
| 26 | + auto end2 = contiguous_iterator(p.data() + p.size()); |
| 27 | + |
| 28 | + benchmark::DoNotOptimize(std::ranges::starts_with(begin1, end1, begin2, end2)); |
| 29 | + } |
| 30 | +} |
| 31 | +BENCHMARK(bm_starts_with_contiguous_iter_with_memcmp_optimization)->RangeMultiplier(16)->Range(16, 16 << 20); |
| 32 | + |
| 33 | +static void bm_starts_with_contiguous_iter(benchmark::State& state) { |
| 34 | + std::vector<int> a(state.range(), 1); |
| 35 | + std::vector<int> p(state.range(), 1); |
| 36 | + |
| 37 | + for (auto _ : state) { |
| 38 | + benchmark::DoNotOptimize(a); |
| 39 | + benchmark::DoNotOptimize(p); |
| 40 | + |
| 41 | + auto begin1 = contiguous_iterator(a.data()); |
| 42 | + auto end1 = contiguous_iterator(a.data() + a.size()); |
| 43 | + auto begin2 = contiguous_iterator(p.data()); |
| 44 | + auto end2 = contiguous_iterator(p.data() + p.size()); |
| 45 | + |
| 46 | + // Using a custom predicate to make sure the memcmp optimization doesn't get invoked |
| 47 | + benchmark::DoNotOptimize( |
| 48 | + std::ranges::starts_with(begin1, end1, begin2, end2, [](const int a, const int b) { return a == b; })); |
| 49 | + } |
| 50 | +} |
| 51 | +BENCHMARK(bm_starts_with_contiguous_iter)->RangeMultiplier(16)->Range(16, 16 << 20); |
| 52 | + |
| 53 | +static void bm_starts_with_random_access_iter(benchmark::State& state) { |
| 54 | + std::vector<int> a(state.range(), 1); |
| 55 | + std::vector<int> p(state.range(), 1); |
| 56 | + |
| 57 | + for (auto _ : state) { |
| 58 | + benchmark::DoNotOptimize(a); |
| 59 | + benchmark::DoNotOptimize(p); |
| 60 | + |
| 61 | + auto begin1 = random_access_iterator(a.data()); |
| 62 | + auto end1 = random_access_iterator(a.data() + a.size()); |
| 63 | + auto begin2 = random_access_iterator(p.data()); |
| 64 | + auto end2 = random_access_iterator(p.data() + p.size()); |
| 65 | + |
| 66 | + benchmark::DoNotOptimize(std::ranges::starts_with(begin1, end1, begin2, end2)); |
| 67 | + } |
| 68 | +} |
| 69 | +BENCHMARK(bm_starts_with_random_access_iter)->RangeMultiplier(16)->Range(16, 16 << 20); |
| 70 | + |
| 71 | +static void bm_starts_with_bidirectional_iter(benchmark::State& state) { |
| 72 | + std::vector<int> a(state.range(), 1); |
| 73 | + std::vector<int> p(state.range(), 1); |
| 74 | + |
| 75 | + for (auto _ : state) { |
| 76 | + benchmark::DoNotOptimize(a); |
| 77 | + benchmark::DoNotOptimize(p); |
| 78 | + |
| 79 | + auto begin1 = bidirectional_iterator(a.data()); |
| 80 | + auto end1 = bidirectional_iterator(a.data() + a.size()); |
| 81 | + auto begin2 = bidirectional_iterator(p.data()); |
| 82 | + auto end2 = bidirectional_iterator(p.data() + p.size()); |
| 83 | + |
| 84 | + benchmark::DoNotOptimize(std::ranges::starts_with(begin1, end1, begin2, end2)); |
| 85 | + } |
| 86 | +} |
| 87 | +BENCHMARK(bm_starts_with_bidirectional_iter)->RangeMultiplier(16)->Range(16, 16 << 20); |
| 88 | + |
| 89 | +static void bm_starts_with_forward_iter(benchmark::State& state) { |
| 90 | + std::vector<int> a(state.range(), 1); |
| 91 | + std::vector<int> p(state.range(), 1); |
| 92 | + |
| 93 | + for (auto _ : state) { |
| 94 | + benchmark::DoNotOptimize(a); |
| 95 | + benchmark::DoNotOptimize(p); |
| 96 | + |
| 97 | + auto begin1 = forward_iterator(a.data()); |
| 98 | + auto end1 = forward_iterator(a.data() + a.size()); |
| 99 | + auto begin2 = forward_iterator(p.data()); |
| 100 | + auto end2 = forward_iterator(p.data() + p.size()); |
| 101 | + |
| 102 | + benchmark::DoNotOptimize(std::ranges::starts_with(begin1, end1, begin2, end2)); |
| 103 | + } |
| 104 | +} |
| 105 | +BENCHMARK(bm_starts_with_forward_iter)->RangeMultiplier(16)->Range(16, 16 << 20); |
| 106 | + |
| 107 | +BENCHMARK_MAIN(); |
0 commit comments