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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
10 | |
11 | #include <algorithm> |
12 | #include <cstddef> |
13 | #include <deque> |
14 | #include <iterator> |
15 | #include <list> |
16 | #include <string> |
17 | #include <vector> |
18 | |
19 | #include "benchmark/benchmark.h" |
20 | #include "../../GenerateInput.h" |
21 | |
22 | int main(int argc, char** argv) { |
23 | auto std_copy_if = [](auto first, auto last, auto out, auto pred) { return std::copy_if(first, last, out, pred); }; |
24 | |
25 | // Benchmark {std,ranges}::copy_if where we copy one out of two element, in alternance. |
26 | // This is basically the worst case for this algorithm, I don't think there are many |
27 | // optimizations that can be applied in this case. |
28 | { |
29 | auto bm = []<class Container>(std::string name, auto copy_if) { |
30 | benchmark::RegisterBenchmark(name, [copy_if](auto& st) { |
31 | std::size_t const n = st.range(0); |
32 | using ValueType = typename Container::value_type; |
33 | Container c; |
34 | std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); }); |
35 | |
36 | std::vector<ValueType> out(n); |
37 | |
38 | for ([[maybe_unused]] auto _ : st) { |
39 | bool do_copy = false; |
40 | auto pred = [&do_copy](auto& element) { |
41 | benchmark::DoNotOptimize(element); |
42 | do_copy = !do_copy; |
43 | return do_copy; |
44 | }; |
45 | benchmark::DoNotOptimize(c); |
46 | benchmark::DoNotOptimize(out); |
47 | auto result = copy_if(c.begin(), c.end(), out.begin(), pred); |
48 | benchmark::DoNotOptimize(result); |
49 | } |
50 | })->Range(8, 1 << 20); |
51 | }; |
52 | bm.operator()<std::vector<int>>(name: "std::copy_if(vector<int>) (every other)" , copy_if: std_copy_if); |
53 | bm.operator()<std::deque<int>>(name: "std::copy_if(deque<int>) (every other)" , copy_if: std_copy_if); |
54 | bm.operator()<std::list<int>>(name: "std::copy_if(list<int>) (every other)" , copy_if: std_copy_if); |
55 | |
56 | bm.operator()<std::vector<int>>("rng::copy_if(vector<int>) (every other)" , std::ranges::copy_if); |
57 | bm.operator()<std::deque<int>>("rng::copy_if(deque<int>) (every other)" , std::ranges::copy_if); |
58 | bm.operator()<std::list<int>>("rng::copy_if(list<int>) (every other)" , std::ranges::copy_if); |
59 | } |
60 | |
61 | // Benchmark {std,ranges}::copy_if where we copy the full range. |
62 | // Copy the full range. |
63 | { |
64 | auto bm = []<class Container>(std::string name, auto copy_if) { |
65 | benchmark::RegisterBenchmark(name, [copy_if](auto& st) { |
66 | std::size_t const n = st.range(0); |
67 | using ValueType = typename Container::value_type; |
68 | Container c; |
69 | std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); }); |
70 | |
71 | std::vector<ValueType> out(n); |
72 | |
73 | for ([[maybe_unused]] auto _ : st) { |
74 | auto pred = [](auto& element) { |
75 | benchmark::DoNotOptimize(element); |
76 | return true; |
77 | }; |
78 | benchmark::DoNotOptimize(c); |
79 | benchmark::DoNotOptimize(out); |
80 | auto result = copy_if(c.begin(), c.end(), out.begin(), pred); |
81 | benchmark::DoNotOptimize(result); |
82 | } |
83 | })->Range(8, 1 << 20); |
84 | }; |
85 | bm.operator()<std::vector<int>>(name: "std::copy_if(vector<int>) (entire range)" , copy_if: std_copy_if); |
86 | bm.operator()<std::deque<int>>(name: "std::copy_if(deque<int>) (entire range)" , copy_if: std_copy_if); |
87 | bm.operator()<std::list<int>>(name: "std::copy_if(list<int>) (entire range)" , copy_if: std_copy_if); |
88 | |
89 | bm.operator()<std::vector<int>>("rng::copy_if(vector<int>) (entire range)" , std::ranges::copy_if); |
90 | bm.operator()<std::deque<int>>("rng::copy_if(deque<int>) (entire range)" , std::ranges::copy_if); |
91 | bm.operator()<std::list<int>>("rng::copy_if(list<int>) (entire range)" , std::ranges::copy_if); |
92 | } |
93 | |
94 | benchmark::Initialize(&argc, argv); |
95 | benchmark::RunSpecifiedBenchmarks(); |
96 | benchmark::Shutdown(); |
97 | return 0; |
98 | } |
99 | |