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

source code of libcxx/benchmarks/join_view.bench.cpp