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
|
//===----------------------------------------------------------------------===//
//
// 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, c++20
#include <algorithm>
#include <cstddef>
#include <deque>
#include <list>
#include <string>
#include <vector>
#include <benchmark/benchmark.h>
#include "../../GenerateInput.h"
int main(int argc, char** argv) {
auto ranges_starts_with_pred = [](auto first1, auto last1, auto first2, auto last2) {
return std::ranges::starts_with(first1, last1, first2, last2, [](auto x, auto y) {
benchmark::DoNotOptimize(x);
benchmark::DoNotOptimize(y);
return x == y;
});
};
// Benchmark ranges::starts_with where we find the mismatching element at the very end (worst case).
{
auto bm = []<class Container>(std::string name, auto starts_with) {
benchmark::RegisterBenchmark(
name,
[starts_with](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 c1(size, x);
Container c2(size, x);
c2.back() = y;
for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c1);
benchmark::DoNotOptimize(c2);
auto result = starts_with(c1.begin(), c1.end(), c2.begin(), c2.end());
benchmark::DoNotOptimize(result);
}
})
->Arg(8)
->Arg(1000) // non power-of-two
->Arg(1024)
->Arg(8192)
->Arg(1 << 20);
};
bm.operator()<std::vector<int>>("rng::starts_with(vector<int>)", std::ranges::starts_with);
bm.operator()<std::deque<int>>("rng::starts_with(deque<int>)", std::ranges::starts_with);
bm.operator()<std::list<int>>("rng::starts_with(list<int>)", std::ranges::starts_with);
bm.operator()<std::vector<int>>("rng::starts_with(vector<int>, pred)", ranges_starts_with_pred);
bm.operator()<std::deque<int>>("rng::starts_with(deque<int>, pred)", ranges_starts_with_pred);
bm.operator()<std::list<int>>("rng::starts_with(list<int>, pred)", ranges_starts_with_pred);
}
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
|