File: bench-jarowinkler.cpp

package info (click to toggle)
rapidfuzz-cpp 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,480 kB
  • sloc: cpp: 30,893; python: 63; makefile: 26; sh: 8
file content (196 lines) | stat: -rw-r--r-- 6,582 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <benchmark/benchmark.h>
#include <random>
#include <rapidfuzz/distance/Jaro.hpp>
#include <string>
#include <vector>

std::string generate(int max_length)
{
    std::string possible_characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    std::random_device rd;
    std::mt19937 engine(rd());
    std::uniform_int_distribution<> dist(0, static_cast<int>(possible_characters.size() - 1));
    std::string ret = "";
    for (int i = 0; i < max_length; i++) {
        int random_index = dist(engine);
        ret += possible_characters[static_cast<size_t>(random_index)];
    }
    return ret;
}

template <typename T>
std::basic_string<T> str_multiply(std::basic_string<T> a, unsigned int b)
{
    std::basic_string<T> output;
    while (b--)
        output += a;

    return output;
}

static void BM_JaroLongSimilarSequence(benchmark::State& state)
{
    size_t len = state.range(0);
    size_t score_cutoff = state.range(1);
    std::string s1 = std::string("a") + str_multiply(std::string("b"), (len - 2)) + std::string("a");
    std::string s2 = str_multiply(std::string("b"), len);

    size_t num = 0;
    for (auto _ : state) {
        benchmark::DoNotOptimize(rapidfuzz::jaro_similarity(s1, s2));
        ++num;
    }

    state.counters["Rate"] = benchmark::Counter(static_cast<double>(num * len), benchmark::Counter::kIsRate);
    state.counters["InvRate"] = benchmark::Counter(static_cast<double>(num * len),
                                                   benchmark::Counter::kIsRate | benchmark::Counter::kInvert);
}

static void BM_JaroLongNonSimilarSequence(benchmark::State& state)
{
    size_t len = state.range(0);
    size_t score_cutoff = state.range(1);
    std::string s1 = str_multiply(std::string("a"), len);
    std::string s2 = str_multiply(std::string("b"), len);

    size_t num = 0;
    for (auto _ : state) {
        benchmark::DoNotOptimize(rapidfuzz::jaro_similarity(s1, s2));
        ++num;
    }

    state.counters["Rate"] = benchmark::Counter(static_cast<double>(num * len), benchmark::Counter::kIsRate);
    state.counters["InvRate"] = benchmark::Counter(static_cast<double>(num * len),
                                                   benchmark::Counter::kIsRate | benchmark::Counter::kInvert);
}

#ifdef RAPIDFUZZ_SIMD
template <size_t MaxLen1, size_t MaxLen2>
static void BM_Jaro_SIMD(benchmark::State& state)
{
    std::vector<std::string> seq1;
    std::vector<std::string> seq2;
    std::vector<double> results(64);
    for (int i = 0; i < 64; i++)
        seq1.push_back(generate(MaxLen1));
    for (int i = 0; i < 10000; i++)
        seq2.push_back(generate(MaxLen2));

    size_t num = 0;
    for (auto _ : state) {
        rapidfuzz::experimental::MultiJaro<MaxLen1> scorer(seq1.size());
        for (const auto& str1 : seq1)
            scorer.insert(str1);

        for (const auto& str2 : seq2)
            scorer.similarity(&results[0], results.size(), str2);

        num += seq1.size() * seq2.size();
    }

    state.counters["Rate"] = benchmark::Counter(static_cast<double>(num), benchmark::Counter::kIsRate);
    state.counters["InvRate"] = benchmark::Counter(static_cast<double>(num),
                                                   benchmark::Counter::kIsRate | benchmark::Counter::kInvert);
}
#endif

template <size_t MaxLen1, size_t MaxLen2>
static void BM_Jaro(benchmark::State& state)
{
    std::vector<std::string> seq1;
    std::vector<std::string> seq2;
    for (int i = 0; i < 256; i++)
        seq1.push_back(generate(MaxLen1));
    for (int i = 0; i < 10000; i++)
        seq2.push_back(generate(MaxLen2));

    size_t num = 0;
    for (auto _ : state) {
        for (size_t j = 0; j < seq2.size(); ++j)
            for (size_t i = 0; i < seq1.size(); ++i)
                benchmark::DoNotOptimize(rapidfuzz::jaro_similarity(seq1[i], seq2[j]));

        num += seq1.size() * seq2.size();
    }

    state.counters["Rate"] = benchmark::Counter(static_cast<double>(num), benchmark::Counter::kIsRate);
    state.counters["InvRate"] = benchmark::Counter(static_cast<double>(num),
                                                   benchmark::Counter::kIsRate | benchmark::Counter::kInvert);
}

template <size_t MaxLen1, size_t MaxLen2>
static void BM_Jaro_Cached(benchmark::State& state)
{
    std::vector<std::string> seq1;
    std::vector<std::string> seq2;
    for (int i = 0; i < 256; i++)
        seq1.push_back(generate(MaxLen1));
    for (int i = 0; i < 10000; i++)
        seq2.push_back(generate(MaxLen2));

    size_t num = 0;
    for (auto _ : state) {
        for (const auto& str1 : seq1) {
            rapidfuzz::CachedJaro<char> scorer(str1);
            for (size_t j = 0; j < seq2.size(); ++j)
                benchmark::DoNotOptimize(scorer.similarity(seq2[j]));
        }
        num += seq1.size() * seq2.size();
    }

    state.counters["Rate"] = benchmark::Counter(static_cast<double>(num), benchmark::Counter::kIsRate);
    state.counters["InvRate"] = benchmark::Counter(static_cast<double>(num),
                                                   benchmark::Counter::kIsRate | benchmark::Counter::kInvert);
}

BENCHMARK_TEMPLATE(BM_Jaro, 8, 8);
BENCHMARK_TEMPLATE(BM_Jaro, 16, 16);
BENCHMARK_TEMPLATE(BM_Jaro, 32, 32);
BENCHMARK_TEMPLATE(BM_Jaro, 64, 64);

BENCHMARK_TEMPLATE(BM_Jaro_Cached, 8, 8);
BENCHMARK_TEMPLATE(BM_Jaro_Cached, 16, 16);
BENCHMARK_TEMPLATE(BM_Jaro_Cached, 32, 32);
BENCHMARK_TEMPLATE(BM_Jaro_Cached, 64, 64);

#ifdef RAPIDFUZZ_SIMD
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 8, 8);
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 16, 16);
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 32, 32);
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 64, 64);
#endif

BENCHMARK_TEMPLATE(BM_Jaro, 8, 1000);
BENCHMARK_TEMPLATE(BM_Jaro, 16, 1000);
BENCHMARK_TEMPLATE(BM_Jaro, 32, 1000);
BENCHMARK_TEMPLATE(BM_Jaro, 64, 1000);

BENCHMARK_TEMPLATE(BM_Jaro_Cached, 8, 1000);
BENCHMARK_TEMPLATE(BM_Jaro_Cached, 16, 1000);
BENCHMARK_TEMPLATE(BM_Jaro_Cached, 32, 1000);
BENCHMARK_TEMPLATE(BM_Jaro_Cached, 64, 1000);

#ifdef RAPIDFUZZ_SIMD
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 8, 1000);
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 16, 1000);
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 32, 1000);
BENCHMARK_TEMPLATE(BM_Jaro_SIMD, 64, 1000);
#endif

BENCHMARK(BM_JaroLongSimilarSequence)
    ->Args({100, 30})
    ->Args({500, 30})
    ->Args({5000, 30})
    ->Args({10000, 30})
    ->Args({20000, 30})
    ->Args({50000, 30});

BENCHMARK(BM_JaroLongNonSimilarSequence)
    ->Args({100, 30})
    ->Args({500, 30})
    ->Args({5000, 30})
    ->Args({10000, 30})
    ->Args({20000, 30})
    ->Args({50000, 30});

BENCHMARK_MAIN();