Skip to content

Commit

Permalink
benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGuteniev committed Oct 27, 2024
1 parent ca1553d commit 8082c9e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
20 changes: 18 additions & 2 deletions benchmarks/src/find_and_count.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include <cstdint>
#include <cstdlib>
#include <ranges>
#include <type_traits>
#include <vector>

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

using namespace std;
Expand All @@ -22,7 +25,9 @@ void bm(benchmark::State& state) {
const auto size = static_cast<size_t>(state.range(0));
const auto pos = static_cast<size_t>(state.range(1));

vector<T> a(size, T{'0'});
constexpr bool is_string_op = Operation == Op::StrFind || Operation == Op::StrRFind;

conditional_t<is_string_op, basic_string<T>, vector<T>> a(size, T{'0'});

if (pos < size) {
a[pos] = T{'1'};
Expand All @@ -33,12 +38,18 @@ void bm(benchmark::State& state) {
}

for (auto _ : state) {
benchmark::DoNotOptimize(a);

if constexpr (Operation == Op::FindSized) {
benchmark::DoNotOptimize(ranges::find(a.begin(), a.end(), T{'1'}));
} else if constexpr (Operation == Op::FindUnsized) {
benchmark::DoNotOptimize(ranges::find(a.begin(), unreachable_sentinel, T{'1'}));
} else if constexpr (Operation == Op::Count) {
benchmark::DoNotOptimize(ranges::count(a.begin(), a.end(), T{'1'}));
} else if constexpr (Operation == Op::StrFind) {
benchmark::DoNotOptimize(a.find(T{'1'}));
} else if constexpr (Operation == Op::StrRFind) {
benchmark::DoNotOptimize(a.rfind(T{'1'}));
}
}
}
Expand All @@ -49,7 +60,6 @@ void common_args(auto bm) {
bm->Args({63, 62})->Args({31, 30})->Args({15, 14})->Args({7, 6});
}


BENCHMARK(bm<uint8_t, Op::FindSized>)->Apply(common_args);
BENCHMARK(bm<uint8_t, Op::FindUnsized>)->Apply(common_args);
BENCHMARK(bm<uint8_t, Op::Count>)->Apply(common_args);
Expand All @@ -63,4 +73,10 @@ BENCHMARK(bm<uint32_t, Op::Count>)->Apply(common_args);
BENCHMARK(bm<uint64_t, Op::FindSized>)->Apply(common_args);
BENCHMARK(bm<uint64_t, Op::Count>)->Apply(common_args);

BENCHMARK(bm<char, Op::StrFind>)->Apply(common_args);
BENCHMARK(bm<char, Op::StrRFind>)->Apply(common_args);

BENCHMARK(bm<wchar_t, Op::StrFind>)->Apply(common_args);
BENCHMARK(bm<wchar_t, Op::StrRFind>)->Apply(common_args);

BENCHMARK_MAIN();
39 changes: 39 additions & 0 deletions benchmarks/src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ void search_default_searcher(benchmark::State& state) {
}
}

template <class T>
void member_find(benchmark::State& state) {
const auto& src_haystack = patterns[static_cast<size_t>(state.range())].data;
const auto& src_needle = patterns[static_cast<size_t>(state.range())].pattern;

const T haystack(src_haystack.begin(), src_haystack.end());
const T needle(src_needle.begin(), src_needle.end());

for (auto _ : state) {
benchmark::DoNotOptimize(haystack);
benchmark::DoNotOptimize(needle);
auto res = haystack.find(needle);
benchmark::DoNotOptimize(res);
}
}

template <class T>
void classic_find_end(benchmark::State& state) {
const auto& src_haystack = patterns[static_cast<size_t>(state.range())].data;
Expand Down Expand Up @@ -143,6 +159,23 @@ void ranges_find_end(benchmark::State& state) {
}
}

template <class T>
void member_rfind(benchmark::State& state) {
const auto& src_haystack = patterns[static_cast<size_t>(state.range())].data;
const auto& src_needle = patterns[static_cast<size_t>(state.range())].pattern;

const T haystack(src_haystack.begin(), src_haystack.end());
const T needle(src_needle.begin(), src_needle.end());

for (auto _ : state) {
benchmark::DoNotOptimize(haystack);
benchmark::DoNotOptimize(needle);
auto res = haystack.rfind(needle);
benchmark::DoNotOptimize(res);
}
}


void common_args(auto bm) {
bm->DenseRange(0, std::size(patterns) - 1, 1);
}
Expand All @@ -158,10 +191,16 @@ BENCHMARK(ranges_search<std::uint16_t>)->Apply(common_args);
BENCHMARK(search_default_searcher<std::uint8_t>)->Apply(common_args);
BENCHMARK(search_default_searcher<std::uint16_t>)->Apply(common_args);

BENCHMARK(member_find<std::string>)->Apply(common_args);
BENCHMARK(member_find<std::wstring>)->Apply(common_args);

BENCHMARK(classic_find_end<std::uint8_t>)->Apply(common_args);
BENCHMARK(classic_find_end<std::uint16_t>)->Apply(common_args);

BENCHMARK(ranges_find_end<std::uint8_t>)->Apply(common_args);
BENCHMARK(ranges_find_end<std::uint16_t>)->Apply(common_args);

BENCHMARK(member_rfind<std::string>)->Apply(common_args);
BENCHMARK(member_rfind<std::wstring>)->Apply(common_args);

BENCHMARK_MAIN();

0 comments on commit 8082c9e

Please sign in to comment.