| 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 <deque> |
| 13 | #include <ranges> |
| 14 | |
| 15 | #include "benchmark/benchmark.h" |
| 16 | |
| 17 | namespace { |
| 18 | void run_sizes(auto benchmark) { |
| 19 | benchmark->Arg(0) |
| 20 | ->Arg(1) |
| 21 | ->Arg(2) |
| 22 | ->Arg(64) |
| 23 | ->Arg(512) |
| 24 | ->Arg(1024) |
| 25 | ->Arg(4000) |
| 26 | ->Arg(4096) |
| 27 | ->Arg(5500) |
| 28 | ->Arg(64000) |
| 29 | ->Arg(65536) |
| 30 | ->Arg(70000); |
| 31 | } |
| 32 | |
| 33 | void BM_join_view_in_vectors(benchmark::State& state) { |
| 34 | auto size = state.range(0); |
| 35 | std::vector<std::vector<int>> input(size, std::vector<int>(32)); |
| 36 | std::ranges::fill(input | std::views::join, 10); |
| 37 | std::vector<int> output; |
| 38 | output.resize(size * 32); |
| 39 | |
| 40 | for (auto _ : state) { |
| 41 | benchmark::DoNotOptimize(input); |
| 42 | benchmark::DoNotOptimize(output); |
| 43 | std::ranges::copy(input | std::views::join, output.begin()); |
| 44 | } |
| 45 | } |
| 46 | BENCHMARK(BM_join_view_in_vectors)->Apply(run_sizes); |
| 47 | |
| 48 | void BM_join_view_out_vectors(benchmark::State& state) { |
| 49 | auto size = state.range(0); |
| 50 | std::vector<std::vector<int>> output(size, std::vector<int>(32)); |
| 51 | std::vector<int> input; |
| 52 | input.resize(size * 32); |
| 53 | std::ranges::fill(input, 10); |
| 54 | |
| 55 | for (auto _ : state) { |
| 56 | benchmark::DoNotOptimize(output); |
| 57 | benchmark::DoNotOptimize(input); |
| 58 | std::ranges::copy(input, (output | std::views::join).begin()); |
| 59 | } |
| 60 | } |
| 61 | BENCHMARK(BM_join_view_out_vectors)->Apply(run_sizes); |
| 62 | |
| 63 | void BM_join_view_deques(benchmark::State& state) { |
| 64 | auto size = state.range(0); |
| 65 | std::deque<std::deque<int>> deque(size, std::deque<int>(32)); |
| 66 | std::ranges::fill(deque | std::views::join, 10); |
| 67 | std::vector<int> output; |
| 68 | output.resize(size * 32); |
| 69 | |
| 70 | for (auto _ : state) { |
| 71 | benchmark::DoNotOptimize(deque); |
| 72 | benchmark::DoNotOptimize(output); |
| 73 | std::ranges::copy(deque | std::views::join, output.begin()); |
| 74 | } |
| 75 | } |
| 76 | BENCHMARK(BM_join_view_deques)->Apply(run_sizes); |
| 77 | } // namespace |
| 78 | |
| 79 | BENCHMARK_MAIN(); |
| 80 | |