File: benchmark_fuzz.py

package info (click to toggle)
rapidfuzz 3.12.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,436 kB
  • sloc: python: 7,571; cpp: 7,481; sh: 30; makefile: 23
file content (117 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download
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
# todo combine benchmarks of scorers into common code base
from __future__ import annotations

import timeit

import pandas as pd


def benchmark(name, func, setup, lengths, count):
    print(f"starting {name}")
    start = timeit.default_timer()
    results = []
    for length in lengths:
        test = timeit.Timer(func, setup=setup.format(length, count))
        results.append(min(test.timeit(number=1) for _ in range(7)) / count)
    stop = timeit.default_timer()
    print(f"finished {name}, Runtime: ", stop - start)
    return results


setup = """
from rapidfuzz import fuzz as rfuzz
from fuzzywuzzy import fuzz
import string
import random
random.seed(18)
characters = string.ascii_letters + string.digits + string.whitespace + string.punctuation
a      = ''.join(random.choice(characters) for _ in range({0}))
b_list = [''.join(random.choice(characters) for _ in range({0})) for _ in range({1})]
"""

lengths = list(range(1, 512, 2))
count = 1000


def scorer_benchmark(funcname):
    time_rapidfuzz = benchmark("rapidfuzz", f"[rfuzz.{funcname}(a, b) for b in b_list]", setup, lengths, count)

    time_fuzzywuzzy = benchmark("fuzzywuzzy", f"[fuzz.{funcname}(a, b) for b in b_list]", setup, lengths, count)

    results = pd.DataFrame(
        data={
            "length": lengths,
            "rapidfuzz": time_rapidfuzz,
            "fuzzywuzzy": time_fuzzywuzzy,
        }
    )

    results.to_csv(f"results/{funcname}.csv", sep=",", index=False)


scorer_benchmark("ratio")
scorer_benchmark("partial_ratio")
scorer_benchmark("token_sort_ratio")
scorer_benchmark("token_set_ratio")
scorer_benchmark("partial_token_sort_ratio")
scorer_benchmark("partial_token_set_ratio")
scorer_benchmark("WRatio")

# token_ratio is unique to RapidFuzz
time_token_ratio = benchmark(
    "token_ratio",
    "[rfuzz.token_ratio(a, b, processor=None) for b in b_list]",
    setup,
    lengths,
    count,
)

# this gets very slow, so only benchmark it for smaller values
time_token_ratio_simple = benchmark(
    "fuzzywuzzy",
    "[max(rfuzz.token_sort_ratio(a, b, processor=None), rfuzz.token_set_ratio(a, b, processor=None)) for b in b_list]",
    setup,
    lengths,
    count,
)

results = pd.DataFrame(
    data={
        "length": lengths,
        "token_ratio": time_token_ratio,
        "max(token_sort_ratio, token_set_ratio)": time_token_ratio_simple,
    }
)

results.to_csv("results/token_ratio.csv", sep=",", index=False)

# partial_token_ratio is unique to RapidFuzz
time_partial_token_ratio = benchmark(
    "token_ratio",
    "[rfuzz.partial_token_ratio(a, b, processor=None) for b in b_list]",
    setup,
    lengths,
    count,
)

# this gets very slow, so only benchmark it for smaller values
time_partial_token_ratio_simple = benchmark(
    "fuzzywuzzy",
    (
        "[max(rfuzz.partial_token_sort_ratio(a, b, processor=None), "
        "rfuzz.partial_token_set_ratio(a, b, processor=None)) for b in b_list]"
    ),
    setup,
    lengths,
    count,
)

results = pd.DataFrame(
    data={
        "length": lengths,
        "partial_token_ratio": time_partial_token_ratio,
        "max(partial_token_sort_ratio, partial_token_set_ratio)": time_partial_token_ratio_simple,
    }
)

results.to_csv("results/partial_token_ratio.csv", sep=",", index=False)