//===----------------------------------------------------------------------===// // // 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 #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char** argv) { auto std_for_each = [](auto first, auto last, auto f) { return std::for_each(first, last, f); }; // {std,ranges}::for_each { auto bm = [](std::string name, auto for_each) { using ElemType = typename Container::value_type; benchmark::RegisterBenchmark( name, [for_each](auto& st) { std::size_t const size = st.range(0); Container c(size, 1); auto first = c.begin(); auto last = c.end(); for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); auto result = for_each(first, last, [](ElemType& x) { x = std::clamp(x, 10, 100); }); benchmark::DoNotOptimize(result); } }) ->Arg(8) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(8192); }; bm.operator()>("std::for_each(vector)", std_for_each); bm.operator()>("std::for_each(deque)", std_for_each); bm.operator()>("std::for_each(list)", std_for_each); } // std::{,range::}for_each for associative containers { auto iterator_bm = []( std::type_identity, std::bool_constant, std::string name) { benchmark::RegisterBenchmark( name, [](auto& st) { Container c; for (std::int64_t i = 0; i != st.range(0); ++i) { if constexpr (IsMapLike) c.emplace(i, i); else c.emplace(i); } for (auto _ : st) { benchmark::DoNotOptimize(c); std::for_each(c.begin(), c.end(), [](auto v) { benchmark::DoNotOptimize(&v); }); } }) ->Arg(8) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(8192); }; iterator_bm(std::type_identity>{}, std::false_type{}, "std::for_each(set::iterator)"); iterator_bm(std::type_identity>{}, std::false_type{}, "std::for_each(multiset::iterator)"); iterator_bm(std::type_identity>{}, std::true_type{}, "std::for_each(map::iterator)"); iterator_bm( std::type_identity>{}, std::true_type{}, "std::for_each(multimap::iterator)"); auto container_bm = []( std::type_identity, std::bool_constant, std::string name) { benchmark::RegisterBenchmark( name, [](auto& st) { Container c; const std::size_t size = st.range(0); for (std::size_t i = 0; i != size; ++i) { if constexpr (IsMapLike) c.emplace(i, i); else c.emplace(i); } for (auto _ : st) { benchmark::DoNotOptimize(c); std::ranges::for_each(c, [](auto v) { benchmark::DoNotOptimize(&v); }); } }) ->Arg(8) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(8192); }; container_bm(std::type_identity>{}, std::false_type{}, "rng::for_each(set)"); container_bm(std::type_identity>{}, std::false_type{}, "rng::for_each(multiset)"); container_bm(std::type_identity>{}, std::true_type{}, "rng::for_each(map)"); container_bm(std::type_identity>{}, std::true_type{}, "rng::for_each(multimap)"); } // {std,ranges}::for_each for join_view { auto bm = [](std::string name, auto for_each) { using C1 = typename Container::value_type; using ElemType = typename C1::value_type; benchmark::RegisterBenchmark( name, [for_each](auto& st) { std::size_t const size = st.range(0); std::size_t const seg_size = 256; std::size_t const segments = (size + seg_size - 1) / seg_size; Container c(segments); for (std::size_t i = 0, n = size; i < segments; ++i, n -= seg_size) { c[i].resize(std::min(seg_size, n), ElemType(1)); } auto view = c | std::views::join; auto first = view.begin(); auto last = view.end(); for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); auto result = for_each(first, last, [](ElemType& x) { x = std::clamp(x, 10, 100); }); benchmark::DoNotOptimize(result); } }) ->Arg(8) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(8192); }; bm.operator()>>("std::for_each(join_view(vector>))", std_for_each); } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }