Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce len() calls #412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions bench/benchmark_cdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ def get_platform():

def benchmark():
words = ["".join(random.choice(string.ascii_letters + string.digits) for _ in range(8)) for _ in range(10000)]
sample_rate = len(words) // 100
len_words = len(words)
sample_rate = len_words // 100
sample = words[::sample_rate]
total = len(words) * len(sample)
len_sample = len(sample)
total = len_words * len_sample

print("System:", get_platform())
print("Words :", len(words))
print("Sample:", len(sample))
print("Words :", len_words)
print("Sample:", len_sample)
print("Total : %s calls\n" % total)

def wrap_cdist(scorer, processor):
Expand Down
11 changes: 7 additions & 4 deletions bench/benchmark_cpdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ def get_platform():

def benchmark():
words = ["".join(random.choice(string.ascii_letters + string.digits) for _ in range(8)) for _ in range(1000000)]
sample_rate = len(words) // 2
len_words = len(words)
sample_rate = len_words // 2
words1 = words[:sample_rate]
words2 = words[sample_rate::]
total = len(words1)
len_words1 = len(words1)
len_words2 = len(words2)
total = len_words1

print("System:", get_platform())
print("Words :", len(words1))
print("Sample:", len(words2))
print("Words :", len_words1)
print("Sample:", len_words2)
print("Total : %s calls\n" % total)

def wrap_cpdist(scorer):
Expand Down
10 changes: 6 additions & 4 deletions bench/benchmark_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ def get_platform():

def benchmark():
words = ["".join(random.choice(string.ascii_letters + string.digits) for _ in range(10)) for _ in range(10000)]
sample_rate = len(words) // 100
len_words = len(words)
sample_rate = len_words // 100
sample = words[::sample_rate]
len_sample = len(sample)

total = len(words) * len(sample)
total = len_words * len_sample

print("System:", get_platform())
print("Words :", len(words))
print("Sample:", len(sample))
print("Words :", len_words)
print("Sample:", len_sample)
print("Total : %s calls\n" % total)

def wrap(f):
Expand Down
9 changes: 5 additions & 4 deletions src/rapidfuzz/fuzz_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,18 @@ def partial_ratio_alignment(

if not s1 and not s2:
return ScoreAlignment(100.0, 0, 0, 0, 0)

s1, s2 = conv_sequences(s1, s2)
if len(s1) <= len(s2):
len1 = len(s1)
len2 = len(s2)
if len1 <= len2:
shorter = s1
longer = s2
else:
shorter = s2
longer = s1

res = _partial_ratio_impl(shorter, longer, score_cutoff / 100)
if res.score != 100 and len(s1) == len(s2):
if res.score != 100 and len1 == len2:
score_cutoff = max(score_cutoff, res.score)
res2 = _partial_ratio_impl(longer, shorter, score_cutoff / 100)
if res2.score > res.score:
Expand All @@ -335,7 +336,7 @@ def partial_ratio_alignment(
if res.score < score_cutoff:
return None

if len(s1) <= len(s2):
if len1 <= len2:
return res

return ScoreAlignment(res.score, res.dest_start, res.dest_end, res.src_start, res.src_end)
Expand Down
10 changes: 6 additions & 4 deletions src/rapidfuzz/process_cpp_impl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ def extract(query, choices, *, scorer=WRatio, processor=None, limit=5, score_cut
cdef RF_Scorer* scorer_context = NULL
cdef RF_ScorerFlags scorer_flags
cdef int64_t c_limit
cdef int64_t choices_len = <int64_t>len(choices)
scorer_kwargs = scorer_kwargs.copy() if scorer_kwargs else {}

setupPandas()
Expand All @@ -1216,14 +1217,15 @@ def extract(query, choices, *, scorer=WRatio, processor=None, limit=5, score_cut
return []

try:
if limit is None or limit > len(choices):
limit = len(choices)
if limit is None or limit > choices_len:
limit = choices_len
except TypeError:
# handle generators. In Theory we could retrieve the length later on while
# preprocessing the choices, but this is good enough for now
choices = list(choices)
if limit is None or limit > len(choices):
limit = len(choices)
choices_len = <int64_t>len(choices)
if limit is None or limit > choices_len:
limit = choices_len

c_limit = limit
if c_limit == 1:
Expand Down
7 changes: 5 additions & 2 deletions src/rapidfuzz/process_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,17 @@ def cpdist(
"""
import numpy as np

if len(queries) != len(choices):
len_queries = len(queries)
len_choices = len(choices)

if len_queries != len_choices:
error_message = "Length of queries and choices must be the same!"
raise ValueError(error_message)

_ = workers, score_hint
scorer_kwargs = scorer_kwargs or {}
dtype = _dtype_to_type_num(dtype, scorer, scorer_kwargs)
results = np.zeros((len(queries),), dtype=dtype)
results = np.zeros((len_queries,), dtype=dtype)

setupPandas()

Expand Down