Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find / count benchmark #4387

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
73 changes: 73 additions & 0 deletions benchmarks/src/find_and_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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>
#include <type_traits>
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

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];

std::fill_n(a, Size, T{'0'});
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
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<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();