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