forked from liuchengxu/vim-clap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fzy_with_rust.py
74 lines (46 loc) · 1.9 KB
/
test_fzy_with_rust.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import itertools
import random
import re
import string
import fuzzymatch_rs
from fzy_impl import fzy_scorer
def fuzzy_match_py(query, candidates):
scored = []
for c in candidates:
score, indices = fzy_scorer(query, c)
if score != float("-inf"):
scored.append({'score': score, 'indices': indices, 'text': c})
ranked = sorted(scored, key=lambda x: x['score'], reverse=True)
indices = []
filtered = []
for r in ranked:
filtered.append(r['text'])
indices.append(r['indices'])
return (indices, filtered)
query = 'sr'
candidates = open('../../test/testdata.txt', 'r').read().split('\n')
print(fuzzy_match_py(query, candidates))
print(fuzzymatch_rs.fuzzy_match(query, candidates))
def test_pure_python_10000(benchmark):
print(benchmark(fuzzy_match_py, query, candidates[:10000]))
def test_rust_10000(benchmark):
print(benchmark(fuzzymatch_rs.fuzzy_match, query, candidates[:10000]))
def test_pure_python_100000(benchmark):
print(benchmark(fuzzy_match_py, query, candidates[:100000]))
def test_rust_100000(benchmark):
print(benchmark(fuzzymatch_rs.fuzzy_match, query, candidates[:100000]))
def test_pure_python_200000(benchmark):
print(benchmark(fuzzy_match_py, query, candidates[:200000]))
def test_rust_200000(benchmark):
print(benchmark(fuzzymatch_rs.fuzzy_match, query, candidates[:200000]))
# This would cost more than 30 seconds for Python.
# def test_pure_python_500000(benchmark):
# print(benchmark(fuzzy_match_py, query, candidates[:500000]))
# def test_rust_500000(benchmark):
# print(benchmark(fuzzymatch_rs.fuzzy_match, query, candidates[:500000]))
# def test_pure_python_800000(benchmark):
# print(benchmark(fuzzy_match_py, query, candidates[:800000]))
# def test_rust_800000(benchmark):
# print(benchmark(fuzzymatch_rs.fuzzy_match, query, candidates[:800000]))