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 | auto compute_median(auto first, auto last) { |
23 | std::vector v(first, last); |
24 | auto middle = v.begin() + v.size() / 2; |
25 | std::nth_element(v.begin(), middle, v.end()); |
26 | return *middle; |
27 | } |
28 | |
29 | int main(int argc, char** argv) { |
30 | auto std_partition_copy = [](auto first, auto last, auto out_yes, auto out_no, auto pred) { |
31 | return std::partition_copy(first, last, out_yes, out_no, pred); |
32 | }; |
33 | |
34 | auto bm = []<class Container>(std::string name, auto partition_copy) { |
35 | benchmark::RegisterBenchmark( |
36 | name, |
37 | [partition_copy](auto& st) { |
38 | std::size_t const size = st.range(0); |
39 | using ValueType = typename Container::value_type; |
40 | Container c; |
41 | std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); }); |
42 | |
43 | std::vector<ValueType> yes(size); |
44 | std::vector<ValueType> no(size); |
45 | ValueType median = compute_median(c.begin(), c.end()); |
46 | auto pred = [median](auto const& element) { return element < median; }; |
47 | |
48 | for ([[maybe_unused]] auto _ : st) { |
49 | benchmark::DoNotOptimize(c); |
50 | benchmark::DoNotOptimize(yes); |
51 | benchmark::DoNotOptimize(no); |
52 | auto result = partition_copy(c.begin(), c.end(), yes.begin(), no.begin(), pred); |
53 | benchmark::DoNotOptimize(result); |
54 | } |
55 | }) |
56 | ->Arg(32) |
57 | ->Arg(50) // non power-of-two |
58 | ->Arg(1024) |
59 | ->Arg(8192); |
60 | }; |
61 | |
62 | // std::partition_copy |
63 | bm.operator()<std::vector<int>>(name: "std::partition_copy(vector<int>)" , partition_copy: std_partition_copy); |
64 | bm.operator()<std::deque<int>>(name: "std::partition_copy(deque<int>)" , partition_copy: std_partition_copy); |
65 | bm.operator()<std::list<int>>(name: "std::partition_copy(list<int>)" , partition_copy: std_partition_copy); |
66 | |
67 | // ranges::partition_copy |
68 | bm.operator()<std::vector<int>>("rng::partition_copy(vector<int>)" , std::ranges::partition_copy); |
69 | bm.operator()<std::deque<int>>("rng::partition_copy(deque<int>)" , std::ranges::partition_copy); |
70 | bm.operator()<std::list<int>>("rng::partition_copy(list<int>)" , std::ranges::partition_copy); |
71 | |
72 | benchmark::Initialize(&argc, argv); |
73 | benchmark::RunSpecifiedBenchmarks(); |
74 | benchmark::Shutdown(); |
75 | return 0; |
76 | } |
77 | |