1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
#include <algorithm>
#include <cstddef>
#include <deque>
#include <initializer_list>
#include <list>
#include <string>
#include <vector>
#include <benchmark/benchmark.h>
#include "../../GenerateInput.h"
int main(int argc, char** argv) {
auto std_find_first_of = [](auto first1, auto last1, auto first2, auto last2) {
return std::find_first_of(first1, last1, first2, last2);
};
auto std_find_first_of_pred = [](auto first1, auto last1, auto first2, auto last2) {
return std::find_first_of(first1, last1, first2, last2, [](auto x, auto y) {
benchmark::DoNotOptimize(x);
benchmark::DoNotOptimize(y);
return x == y;
});
};
auto ranges_find_first_of_pred = [](auto first1, auto last1, auto first2, auto last2) {
return std::ranges::find_first_of(first1, last1, first2, last2, [](auto x, auto y) {
benchmark::DoNotOptimize(x);
benchmark::DoNotOptimize(y);
return x == y;
});
};
// Benchmark {std,ranges}::find_first_of where we never find a match in the needle, and the needle is small.
// This is the worst case of the most common case (a small needle).
{
auto bm = []<class Container>(std::string name, auto find_first_of) {
benchmark::RegisterBenchmark(
name,
[find_first_of](auto& st) {
std::size_t const size = st.range(0);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();
ValueType y = random_different_from({x});
Container haystack(size, x);
Container needle(10, y);
for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(haystack);
benchmark::DoNotOptimize(needle);
auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end());
benchmark::DoNotOptimize(result);
}
})
->Arg(32)
->Arg(50) // non power-of-two
->Arg(1024)
->Arg(8192);
};
// {std,ranges}::find_first_of(it1, it1, it2, it2)
bm.operator()<std::vector<int>>("std::find_first_of(vector<int>) (small needle)", std_find_first_of);
bm.operator()<std::deque<int>>("std::find_first_of(deque<int>) (small needle)", std_find_first_of);
bm.operator()<std::list<int>>("std::find_first_of(list<int>) (small needle)", std_find_first_of);
bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>) (small needle)", std::ranges::find_first_of);
bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>) (small needle)", std::ranges::find_first_of);
bm.operator()<std::list<int>>("rng::find_first_of(list<int>) (small needle)", std::ranges::find_first_of);
// {std,ranges}::find_first_of(it1, it1, it2, it2, pred)
bm.operator()<std::vector<int>>("std::find_first_of(vector<int>, pred) (small needle)", std_find_first_of_pred);
bm.operator()<std::deque<int>>("std::find_first_of(deque<int>, pred) (small needle)", std_find_first_of_pred);
bm.operator()<std::list<int>>("std::find_first_of(list<int>, pred) (small needle)", std_find_first_of_pred);
bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>, pred) (small needle)", ranges_find_first_of_pred);
bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>, pred) (small needle)", ranges_find_first_of_pred);
bm.operator()<std::list<int>>("rng::find_first_of(list<int>, pred) (small needle)", ranges_find_first_of_pred);
}
// Special case: the needle is large compared to the haystack, and we find a match early in the haystack.
{
auto bm = []<class Container>(std::string name, auto find_first_of) {
benchmark::RegisterBenchmark(
name,
[find_first_of](auto& st) {
std::size_t const size = st.range(0);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();
ValueType y = random_different_from({x});
Container haystack(size, x);
Container needle(size * 10, y);
// put a match at 10% of the haystack
*std::next(haystack.begin(), haystack.size() / 10) = y;
for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(haystack);
benchmark::DoNotOptimize(needle);
auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end());
benchmark::DoNotOptimize(result);
}
})
->Arg(32)
->Arg(50) // non power-of-two
->Arg(1024)
->Arg(8192);
};
// {std,ranges}::find_first_of(it1, it1, it2, it2)
bm.operator()<std::vector<int>>("std::find_first_of(vector<int>) (large needle)", std_find_first_of);
bm.operator()<std::deque<int>>("std::find_first_of(deque<int>) (large needle)", std_find_first_of);
bm.operator()<std::list<int>>("std::find_first_of(list<int>) (large needle)", std_find_first_of);
bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>) (large needle)", std::ranges::find_first_of);
bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>) (large needle)", std::ranges::find_first_of);
bm.operator()<std::list<int>>("rng::find_first_of(list<int>) (large needle)", std::ranges::find_first_of);
// {std,ranges}::find_first_of(it1, it1, it2, it2, pred)
bm.operator()<std::vector<int>>("std::find_first_of(vector<int>, pred) (large needle)", std_find_first_of_pred);
bm.operator()<std::deque<int>>("std::find_first_of(deque<int>, pred) (large needle)", std_find_first_of_pred);
bm.operator()<std::list<int>>("std::find_first_of(list<int>, pred) (large needle)", std_find_first_of_pred);
bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>, pred) (large needle)", ranges_find_first_of_pred);
bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>, pred) (large needle)", ranges_find_first_of_pred);
bm.operator()<std::list<int>>("rng::find_first_of(list<int>, pred) (large needle)", ranges_find_first_of_pred);
}
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
|