Skip to content

Commit 79f6caf

Browse files
authored
gh-145131: difflib.Differ._fancy_replace performance (re-use ratio) (#145133)
re-use calculated ratio
1 parent fd01372 commit 79f6caf

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Lib/difflib.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,12 @@ def _fancy_replace(self, a, alo, ahi, b, blo, bhi):
942942
cruncher.set_seq1(a[i])
943943
# Ordering by cheapest to most expensive ratio is very
944944
# valuable, most often getting out early.
945-
if (crqr() > best_ratio
946-
and cqr() > best_ratio
947-
and cr() > best_ratio):
948-
best_i, best_j, best_ratio = i, j, cr()
945+
if crqr() <= best_ratio or cqr() <= best_ratio:
946+
continue
947+
948+
ratio = cr()
949+
if ratio > best_ratio:
950+
best_i, best_j, best_ratio = i, j, ratio
949951

950952
if best_i is None:
951953
# found nothing to synch on yet - move to next j

0 commit comments

Comments
 (0)