brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · e643e64 Raw
94 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 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 21int main(int argc, char** argv) {22  auto std_for_each_n = [](auto first, auto n, auto f) { return std::for_each_n(first, n, f); };23 24  // {std,ranges}::for_each_n25  {26    auto bm = []<class Container>(std::string name, auto for_each_n) {27      using ElemType = typename Container::value_type;28      benchmark::RegisterBenchmark(29          name,30          [for_each_n](auto& st) {31            std::size_t const n = st.range(0);32            Container c(n, 1);33            auto first = c.begin();34 35            for ([[maybe_unused]] auto _ : st) {36              benchmark::DoNotOptimize(c);37              auto result = for_each_n(first, n, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });38              benchmark::DoNotOptimize(result);39            }40          })41          ->Arg(8)42          ->Arg(32)43          ->Arg(50) // non power-of-two44          ->Arg(8192);45    };46    bm.operator()<std::vector<int>>("std::for_each_n(vector<int>)", std_for_each_n);47    bm.operator()<std::deque<int>>("std::for_each_n(deque<int>)", std_for_each_n);48    bm.operator()<std::list<int>>("std::for_each_n(list<int>)", std_for_each_n);49    bm.operator()<std::vector<int>>("rng::for_each_n(vector<int>)", std::ranges::for_each_n);50    bm.operator()<std::deque<int>>("rng::for_each_n(deque<int>)", std::ranges::for_each_n);51    bm.operator()<std::list<int>>("rng::for_each_n(list<int>)", std::ranges::for_each_n);52  }53 54  // {std,ranges}::for_each_n for join_view55  {56    auto bm = []<class Container>(std::string name, auto for_each_n) {57      using C1       = typename Container::value_type;58      using ElemType = typename C1::value_type;59      benchmark::RegisterBenchmark(60          name,61          [for_each_n](auto& st) {62            std::size_t const size     = st.range(0);63            std::size_t const seg_size = 256;64            std::size_t const segments = (size + seg_size - 1) / seg_size;65            Container c(segments);66            for (std::size_t i = 0, n = size; i < segments; ++i, n -= seg_size) {67              c[i].resize(std::min(seg_size, n), ElemType(1));68            }69 70            auto view  = c | std::views::join;71            auto first = view.begin();72 73            for ([[maybe_unused]] auto _ : st) {74              benchmark::DoNotOptimize(c);75              auto result = for_each_n(first, size, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });76              benchmark::DoNotOptimize(result);77            }78          })79          ->Arg(8)80          ->Arg(32)81          ->Arg(50) // non power-of-two82          ->Arg(8192);83    };84    bm.operator()<std::vector<std::vector<int>>>("std::for_each_n(join_view(vector<vector<int>>))", std_for_each_n);85    bm.operator()<std::vector<std::vector<int>>>(86        "rng::for_each_n(join_view(vector<vector<int>>)", std::ranges::for_each_n);87  }88 89  benchmark::Initialize(&argc, argv);90  benchmark::RunSpecifiedBenchmarks();91  benchmark::Shutdown();92  return 0;93}94