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..0b7561d4a9a --- /dev/null +++ b/benchmarks/src/find_and_count.cpp @@ -0,0 +1,72 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include +#include +#include +#include + +enum class Op { + FindSized, + FindUnsized, + Count, +}; + +using namespace std; + +template +void bm(benchmark::State& state) { + T a[Size]; + + 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, 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();