1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include <benchmark/benchmark.h>
13 #include "test_iterators.h"
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);
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);