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 <list>
15#include <ranges>
16#include <string>
17#include <vector>
18
19#include <benchmark/benchmark.h>
20
21int main(int argc, char** argv) {
22 auto std_for_each_n = [](auto first, auto n, auto f) { return std::for_each_n(first, n, f); };
23
24 // std::for_each_n
25 {
26 auto bm = []<class Container>(std::string name, auto for_each_n) {
27 using ElemType = typename Container::value_type;
28 benchmark::RegisterBenchmark(
29 name,
30 [for_each_n](auto& st) {
31 std::size_t const n = st.range(0);
32 Container c(n, 1);
33 auto first = c.begin();
34
35 for ([[maybe_unused]] auto _ : st) {
36 benchmark::DoNotOptimize(c);
37 auto result = for_each_n(first, n, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });
38 benchmark::DoNotOptimize(result);
39 }
40 })
41 ->Arg(8)
42 ->Arg(32)
43 ->Arg(50) // non power-of-two
44 ->Arg(1024)
45 ->Arg(4096)
46 ->Arg(8192)
47 ->Arg(1 << 14)
48 ->Arg(1 << 16)
49 ->Arg(1 << 18);
50 };
51 bm.operator()<std::vector<int>>("std::for_each_n(vector<int>)", std_for_each_n);
52 bm.operator()<std::deque<int>>("std::for_each_n(deque<int>)", std_for_each_n);
53 bm.operator()<std::list<int>>("std::for_each_n(list<int>)", std_for_each_n);
54 }
55
56 // std::for_each_n for join_view
57 {
58 auto bm = []<class Container>(std::string name, auto for_each_n) {
59 using C1 = typename Container::value_type;
60 using ElemType = typename C1::value_type;
61 benchmark::RegisterBenchmark(
62 name,
63 [for_each_n](auto& st) {
64 std::size_t const size = st.range(0);
65 std::size_t const seg_size = 256;
66 std::size_t const segments = (size + seg_size - 1) / seg_size;
67 Container c(segments);
68 for (std::size_t i = 0, n = size; i < segments; ++i, n -= seg_size) {
69 c[i].resize(std::min(seg_size, n), ElemType(1));
70 }
71
72 auto view = c | std::views::join;
73 auto first = view.begin();
74
75 for ([[maybe_unused]] auto _ : st) {
76 benchmark::DoNotOptimize(c);
77 auto result = for_each_n(first, size, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });
78 benchmark::DoNotOptimize(result);
79 }
80 })
81 ->Arg(8)
82 ->Arg(32)
83 ->Arg(50) // non power-of-two
84 ->Arg(1024)
85 ->Arg(4096)
86 ->Arg(8192)
87 ->Arg(1 << 14)
88 ->Arg(1 << 16)
89 ->Arg(1 << 18);
90 };
91 bm.operator()<std::vector<std::vector<int>>>("std::for_each_n(join_view(vector<vector<int>>))", std_for_each_n);
92 }
93
94 benchmark::Initialize(&argc, argv);
95 benchmark::RunSpecifiedBenchmarks();
96 benchmark::Shutdown();
97 return 0;
98}
99

source code of libcxx/test/benchmarks/algorithms/nonmodifying/for_each_n.bench.cpp