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