]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
suggestions.c: Improve efficiency of levenshtein_distance method (#91835)
authorPieter Eendebak <pieter.eendebak@gmail.com>
Mon, 2 May 2022 17:09:35 +0000 (19:09 +0200)
committerGitHub <noreply@github.com>
Mon, 2 May 2022 17:09:35 +0000 (11:09 -0600)
Python/suggestions.c

index d9e69fa7e0db217d1a1c51fba921167408a0fc49..b84acaaed6b1fdb53b8e1fed9fb00ec854a3b030 100644 (file)
@@ -78,9 +78,11 @@ levenshtein_distance(const char *a, size_t a_size,
     // Instead of producing the whole traditional len(a)-by-len(b)
     // matrix, we can update just one row in place.
     // Initialize the buffer row
+    size_t tmp = MOVE_COST;
     for (size_t i = 0; i < a_size; i++) {
         // cost from b[:0] to a[:i+1]
-        buffer[i] = (i + 1) * MOVE_COST;
+        buffer[i] = tmp;
+        tmp += MOVE_COST;
     }
 
     size_t result = 0;