aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/case_insensitive_string/benchmark/compare.cpp
blob: 4788f350f2adc91f2135a01a32ecf26d96a9afec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <library/cpp/case_insensitive_string/ut_gtest/util/locale_guard.h>
#include <library/cpp/case_insensitive_string/case_insensitive_string.h>

#include <benchmark/benchmark.h>

#include <util/generic/string.h>
#include <util/random/random.h>

#include <array>

#include <cerrno>
#include <clocale>
#include <cstring>

namespace {
    template <typename TStrBuf>
    void BenchmarkCompare(benchmark::State& state, TStrBuf s1, TStrBuf s2) {
        for (auto _ : state) {
            benchmark::DoNotOptimize(s1);
            benchmark::DoNotOptimize(s2);
            auto cmp = s1.compare(s2);
            benchmark::DoNotOptimize(cmp);
        }
    }

    char RandomPrintableChar() {
        while (true) {
            unsigned char c = RandomNumber(127u);
            if (std::isprint(c)) {
                return c;
            }
        }
    }

    const std::array<const char*, 2> Locales = {
        "C",
        "ru_RU.CP1251",
    };
}

template <typename TStrBuf>
void CompareEqualStrings(benchmark::State& state) {
    SetRandomSeed(123);

    size_t n = state.range(0);
    size_t locIndex = state.range(1);

    TLocaleGuard loc(Locales[locIndex]);
    if (loc.Error()) {
        state.SkipWithMessage(TString::Join(Locales[locIndex], " locale is not available: ", loc.Error()));
        return;
    }
    TString s1(Reserve(n)), s2(Reserve(n));
    for (size_t i = 0; i < n; ++i) {
        auto c = RandomPrintableChar();
        s1.push_back(std::toupper(c));
        s2.push_back(std::tolower(c));
    }

    BenchmarkCompare(state, TStrBuf(s1.data(), s1.size()), TStrBuf(s2.data(), s2.size()));
}

#define BENCH_ARGS ArgNames({"strlen", "locale"})->ArgsProduct({{2, 4, 8, 16, 32, 64}, {0, 1}})

BENCHMARK(CompareEqualStrings<TCaseInsensitiveStringBuf>)->BENCH_ARGS;
BENCHMARK(CompareEqualStrings<TCaseInsensitiveAsciiStringBuf>)->BENCH_ARGS;

#undef BENCH_ARGS