brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · f58f336 Raw
96 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 = [](auto first, auto last, auto f) { return std::for_each(first, last, f); };23 24  // {std,ranges}::for_each25  {26    auto bm = []<class Container>(std::string name, auto for_each) {27      using ElemType = typename Container::value_type;28      benchmark::RegisterBenchmark(29          name,30          [for_each](auto& st) {31            std::size_t const size = st.range(0);32            Container c(size, 1);33            auto first = c.begin();34            auto last  = c.end();35 36            for ([[maybe_unused]] auto _ : st) {37              benchmark::DoNotOptimize(c);38              auto result = for_each(first, last, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });39              benchmark::DoNotOptimize(result);40            }41          })42          ->Arg(8)43          ->Arg(32)44          ->Arg(50) // non power-of-two45          ->Arg(8192);46    };47    bm.operator()<std::vector<int>>("std::for_each(vector<int>)", std_for_each);48    bm.operator()<std::deque<int>>("std::for_each(deque<int>)", std_for_each);49    bm.operator()<std::list<int>>("std::for_each(list<int>)", std_for_each);50    bm.operator()<std::vector<int>>("rng::for_each(vector<int>)", std::ranges::for_each);51    bm.operator()<std::deque<int>>("rng::for_each(deque<int>)", std::ranges::for_each);52    bm.operator()<std::list<int>>("rng::for_each(list<int>)", std::ranges::for_each);53  }54 55  // {std,ranges}::for_each for join_view56  {57    auto bm = []<class Container>(std::string name, auto for_each) {58      using C1       = typename Container::value_type;59      using ElemType = typename C1::value_type;60 61      benchmark::RegisterBenchmark(62          name,63          [for_each](auto& st) {64            std::size_t const size     = st.range(0);65            std::size_t const seg_size = 256;66            std::size_t const segments = (size + seg_size - 1) / seg_size;67            Container c(segments);68            for (std::size_t i = 0, n = size; i < segments; ++i, n -= seg_size) {69              c[i].resize(std::min(seg_size, n), ElemType(1));70            }71 72            auto view  = c | std::views::join;73            auto first = view.begin();74            auto last  = view.end();75 76            for ([[maybe_unused]] auto _ : st) {77              benchmark::DoNotOptimize(c);78              auto result = for_each(first, last, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });79              benchmark::DoNotOptimize(result);80            }81          })82          ->Arg(8)83          ->Arg(32)84          ->Arg(50) // non power-of-two85          ->Arg(8192);86    };87    bm.operator()<std::vector<std::vector<int>>>("std::for_each(join_view(vector<vector<int>>))", std_for_each);88    bm.operator()<std::vector<std::vector<int>>>("rng::for_each(join_view(vector<vector<int>>)", std::ranges::for_each);89  }90 91  benchmark::Initialize(&argc, argv);92  benchmark::RunSpecifiedBenchmarks();93  benchmark::Shutdown();94  return 0;95}96