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_reverse_copy = [](auto first, auto last, auto out) { return std::reverse_copy(first, last, out); }; |
24 | |
25 | // {std,ranges}::reverse_copy(normal container) |
26 | { |
27 | auto bm = []<class Container>(std::string name, auto reverse_copy) { |
28 | benchmark::RegisterBenchmark(name, [reverse_copy](auto& st) { |
29 | std::size_t const size = st.range(0); |
30 | using ValueType = typename Container::value_type; |
31 | Container c; |
32 | std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); }); |
33 | |
34 | std::vector<ValueType> out(size); |
35 | |
36 | for ([[maybe_unused]] auto _ : st) { |
37 | benchmark::DoNotOptimize(c); |
38 | benchmark::DoNotOptimize(out); |
39 | auto result = reverse_copy(c.begin(), c.end(), out.begin()); |
40 | benchmark::DoNotOptimize(result); |
41 | } |
42 | })->Range(8, 1 << 15); |
43 | }; |
44 | bm.operator()<std::vector<int>>(name: "std::reverse_copy(vector<int>)" , reverse_copy: std_reverse_copy); |
45 | bm.operator()<std::deque<int>>(name: "std::reverse_copy(deque<int>)" , reverse_copy: std_reverse_copy); |
46 | bm.operator()<std::list<int>>(name: "std::reverse_copy(list<int>)" , reverse_copy: std_reverse_copy); |
47 | bm.operator()<std::vector<int>>("rng::reverse_copy(vector<int>)" , std::ranges::reverse_copy); |
48 | bm.operator()<std::deque<int>>("rng::reverse_copy(deque<int>)" , std::ranges::reverse_copy); |
49 | bm.operator()<std::list<int>>("rng::reverse_copy(list<int>)" , std::ranges::reverse_copy); |
50 | } |
51 | |
52 | benchmark::Initialize(&argc, argv); |
53 | benchmark::RunSpecifiedBenchmarks(); |
54 | benchmark::Shutdown(); |
55 | return 0; |
56 | } |
57 | |