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, c++20
10
11#include <algorithm>
12#include <cstddef>
13#include <deque>
14#include <iterator>
15#include <list>
16#include <string>
17#include <type_traits>
18#include <vector>
19
20#include <benchmark/benchmark.h>
21#include "../../GenerateInput.h"
22
23int main(int argc, char** argv) {
24 // ranges::{fold_left,fold_right}
25 {
26 auto bm = []<class Container>(std::string name, auto fold) {
27 benchmark::RegisterBenchmark(
28 name,
29 [fold](auto& st) {
30 std::size_t const size = st.range(0);
31 using ValueType = typename Container::value_type;
32 static_assert(std::is_unsigned_v<ValueType>,
33 "We could encounter UB if signed arithmetic overflows in this benchmark");
34
35 Container c;
36 std::generate_n(std::back_inserter(c), size, [&] { return Generate<ValueType>::random(); });
37 ValueType init = c.back();
38 c.pop_back();
39
40 auto f = [](auto x, auto y) {
41 benchmark::DoNotOptimize(x);
42 benchmark::DoNotOptimize(y);
43 return x + y;
44 };
45
46 for ([[maybe_unused]] auto _ : st) {
47 benchmark::DoNotOptimize(c);
48 benchmark::DoNotOptimize(init);
49 auto result = fold(c.begin(), c.end(), init, f);
50 benchmark::DoNotOptimize(result);
51 }
52 })
53 ->Arg(8)
54 ->Arg(32)
55 ->Arg(50) // non power-of-two
56 ->Arg(8192)
57 ->Arg(1 << 20);
58 };
59 bm.operator()<std::vector<unsigned int>>("rng::fold_left(vector<int>)", std::ranges::fold_left);
60 bm.operator()<std::deque<unsigned int>>("rng::fold_left(deque<int>)", std::ranges::fold_left);
61 bm.operator()<std::list<unsigned int>>("rng::fold_left(list<int>)", std::ranges::fold_left);
62
63 // TODO: fold_right not implemented yet
64 // bm.operator()<std::vector<unsigned int>>("rng::fold_right(vector<int>)", std::ranges::fold_right);
65 // bm.operator()<std::deque<unsigned int>>("rng::fold_right(deque<int>)", std::ranges::fold_right);
66 // bm.operator()<std::list<unsigned int>>("rng::fold_right(list<int>)", std::ranges::fold_right);
67 }
68
69 benchmark::Initialize(&argc, argv);
70 benchmark::RunSpecifiedBenchmarks();
71 benchmark::Shutdown();
72 return 0;
73}
74

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