brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.6 KiB · fca1aaf Raw
134 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 <initializer_list>15#include <list>16#include <string>17#include <vector>18 19#include <benchmark/benchmark.h>20#include "../../GenerateInput.h"21 22int main(int argc, char** argv) {23  auto std_find_first_of = [](auto first1, auto last1, auto first2, auto last2) {24    return std::find_first_of(first1, last1, first2, last2);25  };26  auto std_find_first_of_pred = [](auto first1, auto last1, auto first2, auto last2) {27    return std::find_first_of(first1, last1, first2, last2, [](auto x, auto y) {28      benchmark::DoNotOptimize(x);29      benchmark::DoNotOptimize(y);30      return x == y;31    });32  };33  auto ranges_find_first_of_pred = [](auto first1, auto last1, auto first2, auto last2) {34    return std::ranges::find_first_of(first1, last1, first2, last2, [](auto x, auto y) {35      benchmark::DoNotOptimize(x);36      benchmark::DoNotOptimize(y);37      return x == y;38    });39  };40 41  // Benchmark {std,ranges}::find_first_of where we never find a match in the needle, and the needle is small.42  // This is the worst case of the most common case (a small needle).43  {44    auto bm = []<class Container>(std::string name, auto find_first_of) {45      benchmark::RegisterBenchmark(46          name,47          [find_first_of](auto& st) {48            std::size_t const size = st.range(0);49            using ValueType        = typename Container::value_type;50            ValueType x            = Generate<ValueType>::random();51            ValueType y            = random_different_from({x});52            Container haystack(size, x);53            Container needle(10, y);54 55            for ([[maybe_unused]] auto _ : st) {56              benchmark::DoNotOptimize(haystack);57              benchmark::DoNotOptimize(needle);58              auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end());59              benchmark::DoNotOptimize(result);60            }61          })62          ->Arg(32)63          ->Arg(50) // non power-of-two64          ->Arg(1024)65          ->Arg(8192);66    };67    // {std,ranges}::find_first_of(it1, it1, it2, it2)68    bm.operator()<std::vector<int>>("std::find_first_of(vector<int>) (small needle)", std_find_first_of);69    bm.operator()<std::deque<int>>("std::find_first_of(deque<int>) (small needle)", std_find_first_of);70    bm.operator()<std::list<int>>("std::find_first_of(list<int>) (small needle)", std_find_first_of);71    bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>) (small needle)", std::ranges::find_first_of);72    bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>) (small needle)", std::ranges::find_first_of);73    bm.operator()<std::list<int>>("rng::find_first_of(list<int>) (small needle)", std::ranges::find_first_of);74 75    // {std,ranges}::find_first_of(it1, it1, it2, it2, pred)76    bm.operator()<std::vector<int>>("std::find_first_of(vector<int>, pred) (small needle)", std_find_first_of_pred);77    bm.operator()<std::deque<int>>("std::find_first_of(deque<int>, pred) (small needle)", std_find_first_of_pred);78    bm.operator()<std::list<int>>("std::find_first_of(list<int>, pred) (small needle)", std_find_first_of_pred);79    bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>, pred) (small needle)", ranges_find_first_of_pred);80    bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>, pred) (small needle)", ranges_find_first_of_pred);81    bm.operator()<std::list<int>>("rng::find_first_of(list<int>, pred) (small needle)", ranges_find_first_of_pred);82  }83 84  // Special case: the needle is large compared to the haystack, and we find a match early in the haystack.85  {86    auto bm = []<class Container>(std::string name, auto find_first_of) {87      benchmark::RegisterBenchmark(88          name,89          [find_first_of](auto& st) {90            std::size_t const size = st.range(0);91            using ValueType        = typename Container::value_type;92            ValueType x            = Generate<ValueType>::random();93            ValueType y            = random_different_from({x});94            Container haystack(size, x);95            Container needle(size * 10, y);96 97            // put a match at 10% of the haystack98            *std::next(haystack.begin(), haystack.size() / 10) = y;99 100            for ([[maybe_unused]] auto _ : st) {101              benchmark::DoNotOptimize(haystack);102              benchmark::DoNotOptimize(needle);103              auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end());104              benchmark::DoNotOptimize(result);105            }106          })107          ->Arg(32)108          ->Arg(50) // non power-of-two109          ->Arg(1024)110          ->Arg(8192);111    };112    // {std,ranges}::find_first_of(it1, it1, it2, it2)113    bm.operator()<std::vector<int>>("std::find_first_of(vector<int>) (large needle)", std_find_first_of);114    bm.operator()<std::deque<int>>("std::find_first_of(deque<int>) (large needle)", std_find_first_of);115    bm.operator()<std::list<int>>("std::find_first_of(list<int>) (large needle)", std_find_first_of);116    bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>) (large needle)", std::ranges::find_first_of);117    bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>) (large needle)", std::ranges::find_first_of);118    bm.operator()<std::list<int>>("rng::find_first_of(list<int>) (large needle)", std::ranges::find_first_of);119 120    // {std,ranges}::find_first_of(it1, it1, it2, it2, pred)121    bm.operator()<std::vector<int>>("std::find_first_of(vector<int>, pred) (large needle)", std_find_first_of_pred);122    bm.operator()<std::deque<int>>("std::find_first_of(deque<int>, pred) (large needle)", std_find_first_of_pred);123    bm.operator()<std::list<int>>("std::find_first_of(list<int>, pred) (large needle)", std_find_first_of_pred);124    bm.operator()<std::vector<int>>("rng::find_first_of(vector<int>, pred) (large needle)", ranges_find_first_of_pred);125    bm.operator()<std::deque<int>>("rng::find_first_of(deque<int>, pred) (large needle)", ranges_find_first_of_pred);126    bm.operator()<std::list<int>>("rng::find_first_of(list<int>, pred) (large needle)", ranges_find_first_of_pred);127  }128 129  benchmark::Initialize(&argc, argv);130  benchmark::RunSpecifiedBenchmarks();131  benchmark::Shutdown();132  return 0;133}134