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