Skip to content

Commit

Permalink
Add find / count benchmark (#4387)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
AlexGuteniev and StephanTLavavej authored Feb 16, 2024
1 parent 64e410c commit 2f6e7f8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 72 additions & 0 deletions benchmarks/src/find_and_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <benchmark/benchmark.h>
#include <cstddef>
#include <cstdint>
#include <ranges>

enum class Op {
FindSized,
FindUnsized,
Count,
};

using namespace std;

template <class T, size_t Size, size_t Pos, Op Operation>
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<uint8_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<uint8_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<uint8_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<uint16_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<uint16_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<uint16_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<uint32_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<uint32_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<uint32_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<uint64_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<uint64_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<uint64_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<int8_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<int8_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<int8_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<int16_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<int16_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<int16_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<int32_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<int32_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<int32_t, 8021, 3056, Op::Count>);

BENCHMARK(bm<int64_t, 8021, 3056, Op::FindSized>);
BENCHMARK(bm<int64_t, 8021, 3056, Op::FindUnsized>);
BENCHMARK(bm<int64_t, 8021, 3056, Op::Count>);

BENCHMARK_MAIN();

0 comments on commit 2f6e7f8

Please sign in to comment.