| 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 | #include "test_macros.h" |
| 22 | |
| 23 | int main(int argc, char** argv) { |
| 24 | auto std_copy_backward = [](auto first, auto last, auto out) { return std::copy_backward(first, last, out); }; |
| 25 | |
| 26 | // {std,ranges}::copy_n(normal container) |
| 27 | { |
| 28 | auto bm = []<class Container>(std::string name, auto copy_backward) { |
| 29 | benchmark::RegisterBenchmark(name, [copy_backward](auto& st) { |
| 30 | std::size_t const n = st.range(0); |
| 31 | using ValueType = typename Container::value_type; |
| 32 | Container c; |
| 33 | std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); }); |
| 34 | |
| 35 | std::vector<ValueType> out(n); |
| 36 | |
| 37 | for ([[maybe_unused]] auto _ : st) { |
| 38 | benchmark::DoNotOptimize(c); |
| 39 | benchmark::DoNotOptimize(out); |
| 40 | auto result = copy_backward(c.begin(), c.end(), out.end()); |
| 41 | benchmark::DoNotOptimize(result); |
| 42 | } |
| 43 | })->Range(8, 1 << 20); |
| 44 | }; |
| 45 | bm.operator()<std::vector<int>>("std::copy_backward(vector<int>)" , std_copy_backward); |
| 46 | bm.operator()<std::deque<int>>("std::copy_backward(deque<int>)" , std_copy_backward); |
| 47 | bm.operator()<std::list<int>>("std::copy_backward(list<int>)" , std_copy_backward); |
| 48 | bm.operator()<std::vector<int>>("rng::copy_backward(vector<int>)" , std::ranges::copy_backward); |
| 49 | bm.operator()<std::deque<int>>("rng::copy_backward(deque<int>)" , std::ranges::copy_backward); |
| 50 | bm.operator()<std::list<int>>("rng::copy_backward(list<int>)" , std::ranges::copy_backward); |
| 51 | } |
| 52 | |
| 53 | // {std,ranges}::copy_n(vector<bool>) |
| 54 | { |
| 55 | auto bm = []<bool Aligned>(std::string name, auto copy_backward) { |
| 56 | benchmark::RegisterBenchmark(name, [copy_backward](auto& st) { |
| 57 | std::size_t const n = st.range(0); |
| 58 | std::vector<bool> in(n, true); |
| 59 | std::vector<bool> out(Aligned ? n : n + 8); |
| 60 | benchmark::DoNotOptimize(&in); |
| 61 | auto first = in.begin(); |
| 62 | auto last = in.end(); |
| 63 | auto dst = Aligned ? out.end() : out.end() - 4; |
| 64 | for ([[maybe_unused]] auto _ : st) { |
| 65 | benchmark::DoNotOptimize(in); |
| 66 | benchmark::DoNotOptimize(out); |
| 67 | auto result = copy_backward(first, last, dst); |
| 68 | benchmark::DoNotOptimize(result); |
| 69 | } |
| 70 | })->Range(64, 1 << 20); |
| 71 | }; |
| 72 | bm.operator()<true>("std::copy_backward(vector<bool>) (aligned)" , std_copy_backward); |
| 73 | bm.operator()<false>("std::copy_backward(vector<bool>) (unaligned)" , std_copy_backward); |
| 74 | #if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23 |
| 75 | bm.operator()<true>("rng::copy_backward(vector<bool>) (aligned)" , std::ranges::copy_backward); |
| 76 | bm.operator()<false>("rng::copy_backward(vector<bool>) (unaligned)" , std::ranges::copy_backward); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | benchmark::Initialize(&argc, argv); |
| 81 | benchmark::RunSpecifiedBenchmarks(); |
| 82 | benchmark::Shutdown(); |
| 83 | return 0; |
| 84 | } |
| 85 | |