Skip to content

Instantly share code, notes, and snippets.

@entity1991
Last active April 2, 2018 13:07
Show Gist options
  • Save entity1991/9375c1e8d30fd7c0889c4f3dbf651b28 to your computer and use it in GitHub Desktop.
Save entity1991/9375c1e8d30fd7c0889c4f3dbf651b28 to your computer and use it in GitHub Desktop.
Levenshtein algorithm libraries benchmark
require 'benchmark'
# Levenshtein algorithm written in pure ruby.
# https://github.com/threedaymonk/text
require 'text'
# Levenshtein algorithm in C++ with ruby wrapper
# https://github.com/dbalatero/levenshtein-ffi
require 'levenshtein'
# Finding Levenshtein distance between numbers from 1 to 100
n = 100
numbers = [
'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen',
'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty-one', 'twenty-two',
'twenty-three', 'twenty-four', 'twenty-five', 'twenty-six', 'twenty-seven', 'twenty-eight', 'twenty-nine', 'thirty',
'thirty-one', 'thirty-two', 'thirty-three', 'thirty-four', 'thirty-five', 'thirty-six', 'thirty-seven',
'thirty-eight', 'thirty-nine', 'forty', 'forty-one', 'forty-two', 'forty-three', 'forty-four', 'forty-five',
'forty-six', 'forty-seven', 'forty-eight', 'forty-nine', 'fifty', 'fifty-one', 'fifty-two', 'fifty-three',
'fifty-four', 'fifty-five', 'fifty-six', 'fifty-seven', 'fifty-eight', 'fifty-nine', 'sixty', 'sixty-one',
'sixty-two', 'sixty-three', 'sixty-four', 'sixty-five', 'sixty-six', 'sixty-seven', 'sixty-eight', 'sixty-nine',
'seventy', 'seventy-one', 'seventy-two', 'seventy-three', 'seventy-four', 'seventy-five', 'seventy-six',
'seventy-seven', 'seventy-eight', 'seventy-nine', 'eighty', 'eighty-one', 'eighty-two', 'eighty-three',
'eighty-four', 'eighty-five', 'eighty-six', 'eighty-seven', 'eighty-eight', 'eighty-nine', 'ninety', 'ninety-one',
'ninety-two', 'ninety-three', 'ninety-four', 'ninety-five', 'ninety-six', 'ninety-seven', 'ninety-eight',
'ninety-nine', 'one hundred'
]
Benchmark.bm(10) do |x|
x.report('Ruby') do
numbers.each do |number|
numbers.each do |number_co_compare|
n.times { Text::Levenshtein.distance(number, number_co_compare) }
end
end
end
x.report('C++') do
numbers.each do |number|
numbers.each do |number_co_compare|
n.times { Levenshtein.distance(number, number_co_compare) }
end
end
end
end
# OUTPUT
#
# user system total real
# Ruby 22.464000 0.000000 22.464000 ( 22.465976)
# C++ 0.656000 0.000000 0.656000 ( 0.652562)
#
# There is an obviously C++ advantage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment