//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 #include #include #include #include #include #include #include #include #include #include #include "../../GenerateInput.h" int main(int argc, char** argv) { auto std_ranges_fold_left = [](auto first, auto last, auto init, auto func) { return std::ranges::fold_left(first, last, init, func); }; auto std_ranges_fold_left_first = [](auto first, auto last, auto, auto func) { return std::ranges::fold_left_first(first, last, func); }; auto std_ranges_fold_right = [](auto first, auto last, auto init, auto func) { return std::ranges::fold_right(first, last, init, func); }; auto std_ranges_fold_right_last = [](auto first, auto last, auto, auto func) { return std::ranges::fold_right_last(first, last, func); }; // ranges::{fold_left,fold_left_first,fold_right,fold_right_last} { auto bm = [](std::string name, auto fold) { benchmark::RegisterBenchmark( name, [fold](auto& st) { std::size_t const size = st.range(0); using ValueType = typename Container::value_type; static_assert(std::is_unsigned_v, "We could encounter UB if signed arithmetic overflows in this benchmark"); Container c; std::generate_n(std::back_inserter(c), size, [&] { return Generate::random(); }); ValueType init = c.back(); c.pop_back(); auto f = [](auto x, auto y) { benchmark::DoNotOptimize(x); benchmark::DoNotOptimize(y); return x + y; }; for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); benchmark::DoNotOptimize(init); auto result = fold(c.begin(), c.end(), init, f); benchmark::DoNotOptimize(result); } }) ->Arg(8) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(8192) ->Arg(1 << 20); }; bm.operator()>("rng::fold_left(vector)", std_ranges_fold_left); bm.operator()>("rng::fold_left(deque)", std_ranges_fold_left); bm.operator()>("rng::fold_left(list)", std_ranges_fold_left); bm.operator()>("rng::fold_left_first(vector)", std_ranges_fold_left_first); bm.operator()>("rng::fold_left_first(deque)", std_ranges_fold_left_first); bm.operator()>("rng::fold_left_first(list)", std_ranges_fold_left_first); bm.operator()>("rng::fold_right(vector)", std_ranges_fold_right); bm.operator()>("rng::fold_right(deque)", std_ranges_fold_right); bm.operator()>("rng::fold_right(list)", std_ranges_fold_right); bm.operator()>("rng::fold_right_last(vector)", std_ranges_fold_right_last); bm.operator()>("rng::fold_right_last(deque)", std_ranges_fold_right_last); bm.operator()>("rng::fold_right_last(list)", std_ranges_fold_right_last); } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }