//===----------------------------------------------------------------------===// // // 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 "../../GenerateInput.h" int main(int argc, char** argv) { auto std_find_first_of = [](auto first1, auto last1, auto first2, auto last2) { return std::find_first_of(first1, last1, first2, last2); }; auto std_find_first_of_pred = [](auto first1, auto last1, auto first2, auto last2) { return std::find_first_of(first1, last1, first2, last2, [](auto x, auto y) { benchmark::DoNotOptimize(x); benchmark::DoNotOptimize(y); return x == y; }); }; auto ranges_find_first_of_pred = [](auto first1, auto last1, auto first2, auto last2) { return std::ranges::find_first_of(first1, last1, first2, last2, [](auto x, auto y) { benchmark::DoNotOptimize(x); benchmark::DoNotOptimize(y); return x == y; }); }; // Benchmark {std,ranges}::find_first_of where we never find a match in the needle, and the needle is small. // This is the worst case of the most common case (a small needle). { auto bm = [](std::string name, auto find_first_of) { benchmark::RegisterBenchmark( name, [find_first_of](auto& st) { std::size_t const size = st.range(0); using ValueType = typename Container::value_type; ValueType x = Generate::random(); ValueType y = random_different_from({x}); Container haystack(size, x); Container needle(10, y); for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(haystack); benchmark::DoNotOptimize(needle); auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end()); benchmark::DoNotOptimize(result); } }) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(1024) ->Arg(8192); }; // {std,ranges}::find_first_of(it1, it1, it2, it2) bm.operator()>("std::find_first_of(vector) (small needle)", std_find_first_of); bm.operator()>("std::find_first_of(deque) (small needle)", std_find_first_of); bm.operator()>("std::find_first_of(list) (small needle)", std_find_first_of); bm.operator()>("rng::find_first_of(vector) (small needle)", std::ranges::find_first_of); bm.operator()>("rng::find_first_of(deque) (small needle)", std::ranges::find_first_of); bm.operator()>("rng::find_first_of(list) (small needle)", std::ranges::find_first_of); // {std,ranges}::find_first_of(it1, it1, it2, it2, pred) bm.operator()>("std::find_first_of(vector, pred) (small needle)", std_find_first_of_pred); bm.operator()>("std::find_first_of(deque, pred) (small needle)", std_find_first_of_pred); bm.operator()>("std::find_first_of(list, pred) (small needle)", std_find_first_of_pred); bm.operator()>("rng::find_first_of(vector, pred) (small needle)", ranges_find_first_of_pred); bm.operator()>("rng::find_first_of(deque, pred) (small needle)", ranges_find_first_of_pred); bm.operator()>("rng::find_first_of(list, pred) (small needle)", ranges_find_first_of_pred); } // Special case: the needle is large compared to the haystack, and we find a match early in the haystack. { auto bm = [](std::string name, auto find_first_of) { benchmark::RegisterBenchmark( name, [find_first_of](auto& st) { std::size_t const size = st.range(0); using ValueType = typename Container::value_type; ValueType x = Generate::random(); ValueType y = random_different_from({x}); Container haystack(size, x); Container needle(size * 10, y); // put a match at 10% of the haystack *std::next(haystack.begin(), haystack.size() / 10) = y; for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(haystack); benchmark::DoNotOptimize(needle); auto result = find_first_of(haystack.begin(), haystack.end(), needle.begin(), needle.end()); benchmark::DoNotOptimize(result); } }) ->Arg(32) ->Arg(50) // non power-of-two ->Arg(1024) ->Arg(8192); }; // {std,ranges}::find_first_of(it1, it1, it2, it2) bm.operator()>("std::find_first_of(vector) (large needle)", std_find_first_of); bm.operator()>("std::find_first_of(deque) (large needle)", std_find_first_of); bm.operator()>("std::find_first_of(list) (large needle)", std_find_first_of); bm.operator()>("rng::find_first_of(vector) (large needle)", std::ranges::find_first_of); bm.operator()>("rng::find_first_of(deque) (large needle)", std::ranges::find_first_of); bm.operator()>("rng::find_first_of(list) (large needle)", std::ranges::find_first_of); // {std,ranges}::find_first_of(it1, it1, it2, it2, pred) bm.operator()>("std::find_first_of(vector, pred) (large needle)", std_find_first_of_pred); bm.operator()>("std::find_first_of(deque, pred) (large needle)", std_find_first_of_pred); bm.operator()>("std::find_first_of(list, pred) (large needle)", std_find_first_of_pred); bm.operator()>("rng::find_first_of(vector, pred) (large needle)", ranges_find_first_of_pred); bm.operator()>("rng::find_first_of(deque, pred) (large needle)", ranges_find_first_of_pred); bm.operator()>("rng::find_first_of(list, pred) (large needle)", ranges_find_first_of_pred); } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }