Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / benchmarks / algorithms / ranges_ends_with.bench.cpp
blob049af7c2e15a35705db6caed891a56640bc83231
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 //===----------------------------------------------------------------------===//
9 #include <algorithm>
10 #include <benchmark/benchmark.h>
11 #include <iterator>
13 #include "test_iterators.h"
14 #include <vector>
16 static void bm_ends_with_contiguous_iter(benchmark::State& state) {
17 std::vector<int> a(state.range(), 1);
18 std::vector<int> p(state.range(), 1);
20 for (auto _ : state) {
21 benchmark::DoNotOptimize(a);
22 benchmark::DoNotOptimize(p);
24 auto begin1 = contiguous_iterator(a.data());
25 auto end1 = contiguous_iterator(a.data() + a.size());
26 auto begin2 = contiguous_iterator(p.data());
27 auto end2 = contiguous_iterator(p.data() + p.size());
29 benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
32 BENCHMARK(bm_ends_with_contiguous_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
34 static void bm_ends_with_random_iter(benchmark::State& state) {
35 std::vector<int> a(state.range(), 1);
36 std::vector<int> p(state.range(), 1);
38 for (auto _ : state) {
39 benchmark::DoNotOptimize(a);
40 benchmark::DoNotOptimize(p);
42 auto begin1 = random_access_iterator(a.begin());
43 auto end1 = random_access_iterator(a.end());
44 auto begin2 = random_access_iterator(p.begin());
45 auto end2 = random_access_iterator(p.end());
47 benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
50 BENCHMARK(bm_ends_with_random_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
52 static void bm_ends_with_bidirectional_iter(benchmark::State& state) {
53 std::vector<int> a(state.range(), 1);
54 std::vector<int> p(state.range(), 1);
56 for (auto _ : state) {
57 benchmark::DoNotOptimize(a);
58 benchmark::DoNotOptimize(p);
60 auto begin1 = bidirectional_iterator(a.begin());
61 auto end1 = bidirectional_iterator(a.end());
62 auto begin2 = bidirectional_iterator(p.begin());
63 auto end2 = bidirectional_iterator(p.end());
65 benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
68 BENCHMARK(bm_ends_with_bidirectional_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
70 static void bm_ends_with_forward_iter(benchmark::State& state) {
71 std::vector<int> a(state.range(), 1);
72 std::vector<int> p(state.range(), 1);
74 for (auto _ : state) {
75 benchmark::DoNotOptimize(a);
76 benchmark::DoNotOptimize(p);
78 auto begin1 = forward_iterator(a.begin());
79 auto end1 = forward_iterator(a.end());
80 auto begin2 = forward_iterator(p.begin());
81 auto end2 = forward_iterator(p.end());
83 benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
86 BENCHMARK(bm_ends_with_forward_iter)->RangeMultiplier(16)->Range(16, 16 << 20);
88 static void bm_ends_with_forward_iter_with_size_optimization(benchmark::State& state) {
89 std::vector<int> a(state.range(), 1);
90 std::vector<int> p(state.range(), 1);
91 p.push_back(2);
93 for (auto _ : state) {
94 benchmark::DoNotOptimize(a);
95 benchmark::DoNotOptimize(p);
97 auto begin1 = forward_iterator(a.begin());
98 auto end1 = forward_iterator(a.end());
99 auto begin2 = forward_iterator(p.begin());
100 auto end2 = forward_iterator(p.end());
102 benchmark::DoNotOptimize(std::ranges::ends_with(begin1, end1, begin2, end2));
105 BENCHMARK(bm_ends_with_forward_iter_with_size_optimization)->RangeMultiplier(16)->Range(16, 16 << 20);
107 BENCHMARK_MAIN();