Permalink
Browse files

speed up levenshtein_distance about 18%

  • Loading branch information...
tenderlove committed Feb 6, 2014
1 parent 3082aef commit 1ab5475e66c0c3f0e09fd9ea7f416d6b02c40914
Showing with 3 additions and 3 deletions.
  1. +3 −3 lib/rubygems/text.rb
View
@@ -52,11 +52,11 @@ def levenshtein_distance str1, str2
d = (0..m).to_a
x = nil
- n.times do |i|
+ str1.each_char.each_with_index do |char1,i|
e = i+1
- m.times do |j|
- cost = (s[i] == t[j]) ? 0 : 1
+ str2.each_char.each_with_index do |char2,j|
+ cost = (char1 == char2) ? 0 : 1
x = min3(
d[j+1] + 1, # insertion
e + 1, # deletion

0 comments on commit 1ab5475

Please sign in to comment.