From e08f7d177bf59382536ef07074f70c0c8dc5de69 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Mon, 12 Feb 2024 20:04:59 +0200 Subject: [PATCH 1/2] Add `find` / `count` benchmark Ported from #2434 description, dropping unnecessary parts --- benchmarks/CMakeLists.txt | 1 + benchmarks/src/find_and_count.cpp | 73 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 benchmarks/src/find_and_count.cpp diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 1a5071ea583..4b586a786d1 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -109,6 +109,7 @@ function(add_benchmark name) endfunction() add_benchmark(bitset_to_string src/bitset_to_string.cpp) +add_benchmark(find_and_count src/find_and_count.cpp) add_benchmark(locale_classic src/locale_classic.cpp) add_benchmark(minmax_element src/minmax_element.cpp) add_benchmark(path_lexically_normal src/path_lexically_normal.cpp) diff --git a/benchmarks/src/find_and_count.cpp b/benchmarks/src/find_and_count.cpp new file mode 100644 index 00000000000..1126f3f2b01 --- /dev/null +++ b/benchmarks/src/find_and_count.cpp @@ -0,0 +1,73 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include +#include +#include +#include +#include + +enum class Op { + FindSized, + FindUnsized, + Count, +}; + +using namespace std; + +template +void bm(benchmark::State& state) { + T a[Size]; + + std::fill_n(a, Size, T{'0'}); + if constexpr (Pos < Size) { + a[Pos] = T{'1'}; + } else { + static_assert(Operation != Op::FindUnsized); + } + + for (auto _ : state) { + if constexpr (Operation == Op::FindSized) { + benchmark::DoNotOptimize(ranges::find(a, a + Size, T{'1'})); + } else if constexpr (Operation == Op::FindUnsized) { + benchmark::DoNotOptimize(ranges::find(a, std::unreachable_sentinel, T{'1'})); + } else if constexpr (Operation == Op::Count) { + benchmark::DoNotOptimize(ranges::count(a, a + Size, T{'1'})); + } + } +} + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK(bm); +BENCHMARK(bm); +BENCHMARK(bm); + +BENCHMARK_MAIN(); From f10b441a71f77f74883355a9830fa8e6f2596e05 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 12 Feb 2024 15:27:14 -0800 Subject: [PATCH 2/2] Code review nitpicks. --- benchmarks/src/find_and_count.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/benchmarks/src/find_and_count.cpp b/benchmarks/src/find_and_count.cpp index 1126f3f2b01..0b7561d4a9a 100644 --- a/benchmarks/src/find_and_count.cpp +++ b/benchmarks/src/find_and_count.cpp @@ -6,7 +6,6 @@ #include #include #include -#include enum class Op { FindSized, @@ -20,7 +19,7 @@ template void bm(benchmark::State& state) { T a[Size]; - std::fill_n(a, Size, T{'0'}); + fill_n(a, Size, T{'0'}); if constexpr (Pos < Size) { a[Pos] = T{'1'}; } else { @@ -31,7 +30,7 @@ void bm(benchmark::State& state) { if constexpr (Operation == Op::FindSized) { benchmark::DoNotOptimize(ranges::find(a, a + Size, T{'1'})); } else if constexpr (Operation == Op::FindUnsized) { - benchmark::DoNotOptimize(ranges::find(a, std::unreachable_sentinel, T{'1'})); + benchmark::DoNotOptimize(ranges::find(a, unreachable_sentinel, T{'1'})); } else if constexpr (Operation == Op::Count) { benchmark::DoNotOptimize(ranges::count(a, a + Size, T{'1'})); }