diff options
author | Yuki Nishijima <mail@yukinishijima.net> | 2015-04-11 15:16:10 -0700 |
---|---|---|
committer | Yuki Nishijima <mail@yukinishijima.net> | 2015-04-11 15:28:23 -0700 |
commit | f7ba69436acc15da88a94b6af55076d3c7c912ca (patch) | |
tree | a8119ea8db346e09898b97c3cc01b112c8396eb5 /railties/lib | |
parent | 193297abc844da71cfc8f470c593c6c7dfd03af5 (diff) | |
download | rails-f7ba69436acc15da88a94b6af55076d3c7c912ca.tar.gz rails-f7ba69436acc15da88a94b6af55076d3c7c912ca.tar.bz2 rails-f7ba69436acc15da88a94b6af55076d3c7c912ca.zip |
Speed up Levenshtein by 50% and reduce 97% of memory usage
Calculating -------------------------------------
each_char 924.000 i/100ms
each_codepoint 1.381k i/100ms
-------------------------------------------------
each_char 9.320k (¡Þ 5.1%) i/s - 47.124k
each_codepoint 13.857k (¡Þ 3.6%) i/s - 70.431k
Comparison:
each_codepoint: 13857.4 i/s
each_char: 9319.5 i/s - 1.49x slower
The full report can be found here:
https://gist.github.com/yuki24/a80988f35aceac76f1d5
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/generators.rb | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 341291f08b..a7da92168d 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -267,10 +267,13 @@ module Rails d = (0..m).to_a x = nil - str1.each_char.each_with_index do |char1,i| + # avoid duplicating an enumerable object in the loop + str2_codepoint_enumerable = str2.each_codepoint + + str1.each_codepoint.with_index do |char1, i| e = i+1 - str2.each_char.each_with_index do |char2,j| + str2_codepoint_enumerable.with_index do |char2, j| cost = (char1 == char2) ? 0 : 1 x = [ d[j+1] + 1, # insertion |