//===----------------------------------------------------------------------===// // // 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 "../../GenerateInput.h" int main(int argc, char** argv) { auto std_find = [](auto first, auto last, auto const& value) { return std::find(first, last, value); }; auto std_find_if = [](auto first, auto last, auto const& value) { return std::find_if(first, last, [&](auto element) { benchmark::DoNotOptimize(element); return element == value; }); }; auto std_find_if_not = [](auto first, auto last, auto const& value) { return std::find_if_not(first, last, [&](auto element) { benchmark::DoNotOptimize(element); return element != value; }); }; auto ranges_find = [](auto first, auto last, auto const& value) { return std::ranges::find(first, last, value); }; auto ranges_find_if = [](auto first, auto last, auto const& value) { return std::ranges::find_if(first, last, [&](auto element) { benchmark::DoNotOptimize(element); return element == value; }); }; auto ranges_find_if_not = [](auto first, auto last, auto const& value) { return std::ranges::find_if_not(first, last, [&](auto element) { benchmark::DoNotOptimize(element); return element != value; }); }; auto register_benchmarks = [&](auto bm, std::string comment) { // find bm.template operator()>("std::find(vector) (" + comment + ")", std_find); bm.template operator()>("std::find(vector) (" + comment + ")", std_find); bm.template operator()>("std::find(deque) (" + comment + ")", std_find); bm.template operator()>("std::find(list) (" + comment + ")", std_find); bm.template operator()>("rng::find(vector) (" + comment + ")", ranges_find); bm.template operator()>("rng::find(vector) (" + comment + ")", ranges_find); bm.template operator()>("rng::find(deque) (" + comment + ")", ranges_find); bm.template operator()>("rng::find(list) (" + comment + ")", ranges_find); // find_if bm.template operator()>("std::find_if(vector) (" + comment + ")", std_find_if); bm.template operator()>("std::find_if(vector) (" + comment + ")", std_find_if); bm.template operator()>("std::find_if(deque) (" + comment + ")", std_find_if); bm.template operator()>("std::find_if(list) (" + comment + ")", std_find_if); bm.template operator()>("rng::find_if(vector) (" + comment + ")", ranges_find_if); bm.template operator()>("rng::find_if(vector) (" + comment + ")", ranges_find_if); bm.template operator()>("rng::find_if(deque) (" + comment + ")", ranges_find_if); bm.template operator()>("rng::find_if(list) (" + comment + ")", ranges_find_if); // find_if_not bm.template operator()>("std::find_if_not(vector) (" + comment + ")", std_find_if_not); bm.template operator()>("std::find_if_not(vector) (" + comment + ")", std_find_if_not); bm.template operator()>("std::find_if_not(deque) (" + comment + ")", std_find_if_not); bm.template operator()>("std::find_if_not(list) (" + comment + ")", std_find_if_not); bm.template operator()>("rng::find_if_not(vector) (" + comment + ")", ranges_find_if_not); bm.template operator()>("rng::find_if_not(vector) (" + comment + ")", ranges_find_if_not); bm.template operator()>("rng::find_if_not(deque) (" + comment + ")", ranges_find_if_not); bm.template operator()>("rng::find_if_not(list) (" + comment + ")", ranges_find_if_not); }; // Benchmark {std,ranges}::{find,find_if,find_if_not}(normal container) where we // bail out after 25% of elements { auto bm = [](std::string name, auto find) { benchmark::RegisterBenchmark( name, [find](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 c(size, x); // put the element we're searching for at 25% of the sequence *std::next(c.begin(), size / 4) = y; for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); benchmark::DoNotOptimize(y); auto result = find(c.begin(), c.end(), y); benchmark::DoNotOptimize(result); } }) ->Arg(8) ->Arg(1024) ->Arg(8192) ->Arg(1 << 15); }; register_benchmarks(bm, "bail 25%"); } // Benchmark {std,ranges}::{find,find_if,find_if_not}(normal container) where we process the whole sequence { auto bm = [](std::string name, auto find) { benchmark::RegisterBenchmark( name, [find](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 c(size, x); for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); benchmark::DoNotOptimize(y); auto result = find(c.begin(), c.end(), y); benchmark::DoNotOptimize(result); } }) ->Arg(8) ->Arg(50) // non power-of-two ->Arg(1024) ->Arg(8192) ->Arg(1 << 15); }; register_benchmarks(bm, "process all"); } // Benchmark {std,ranges}::{find,find_if,find_if_not}(vector) where we process the whole sequence { auto bm = [](std::string name, auto find) { benchmark::RegisterBenchmark( name, [find](auto& st) { std::size_t const size = st.range(0); std::vector c(size, true); bool y = false; for ([[maybe_unused]] auto _ : st) { benchmark::DoNotOptimize(c); benchmark::DoNotOptimize(y); auto result = find(c.begin(), c.end(), y); benchmark::DoNotOptimize(result); } }) ->Arg(8) ->Arg(50) // non power-of-two ->Arg(1024) ->Arg(8192) ->Arg(1 << 20); }; bm("std::find(vector) (process all)", std_find); bm("rng::find(vector) (process all)", ranges_find); bm("std::find_if(vector) (process all)", std_find_if); bm("rng::find_if(vector) (process all)", ranges_find_if); bm("std::find_if_not(vector) (process all)", std_find_if_not); bm("rng::find_if_not(vector) (process all)", ranges_find_if_not); } benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }