skroutz/string_metric — pull request #15 by charkost (2014-02-01 15:44:17+00:00)
Title: Increase performance of :two_matrix_rows about 7% for MRI

Hello again!

Here we have another one performance increase for MRI.

My tests showed that indexing a String is slower than indexing an Array.

Since we index the "from" and "to" strings inside big and nested iterations it ended up 7% faster converting those 2 strings to array of characters. Replacing #fetch with square brackets for the indexing of options also helped a bit.

For jruby and rbx this optimizations does not work (again).

And the benchmarks:
(IterativeWithTwoMatrixRowsOptimizedMore was temporary class with the new changes in order to make more representative benchmarks)

Rehearsal ---------------------------------------------------------------------------------------------------------------------
StringMetric::Levenshtein::Experiment implementation 2.100000 0.010000 2.110000 ( 2.143954)
StringMetric::Levenshtein::IterativeWithFullMatrix implementation 2.340000 0.010000 2.350000 ( 2.379848)
StringMetric::Levenshtein::IterativeWithTwoMatrixRows implementation 2.060000 0.010000 2.070000 ( 2.102046)
StringMetric::Levenshtein::IterativeWithTwoMatrixRowsOptimized implementation 1.710000 0.000000 1.710000 ( 1.720389)
StringMetric::Levenshtein::IterativeWithTwoMatrixRowsOptimizedMore implementation 1.590000 0.000000 1.590000 ( 1.583849)
Text::Levenshtein implementation 2.260000 0.000000 2.260000 ( 2.269622)
----------------------------------------------------------------------------------------------------------- total: 12.090000sec

user system total real
StringMetric::Levenshtein::Experiment implementation 2.100000 0.000000 2.100000 ( 2.095981)
StringMetric::Levenshtein::IterativeWithFullMatrix implementation 2.300000 0.000000 2.300000 ( 2.296880)
StringMetric::Levenshtein::IterativeWithTwoMatrixRows implementation 2.010000 0.000000 2.010000 ( 2.016962)
StringMetric::Levenshtein::IterativeWithTwoMatrixRowsOptimized implementation 1.700000 0.010000 1.710000 ( 1.713488)
StringMetric::Levenshtein::IterativeWithTwoMatrixRowsOptimizedMore implementation 1.570000 0.000000 1.570000 ( 1.563868)
Text::Levenshtein implementation 2.210000 0.000000 2.210000 ( 2.209920)