//===----------------------------------------------------------------------===// // // 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 "../../GenerateInput.h" int main(int argc, char** argv) { // ranges::{fold_left,fold_right} { 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); // TODO: fold_right not implemented yet // 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); } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }