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