Skip to content

Instantly share code, notes, and snippets.

@Vasfed
Last active December 29, 2015 14:53
Show Gist options
  • Save Vasfed/1d0c7f144d4941f31061 to your computer and use it in GitHub Desktop.
Save Vasfed/1d0c7f144d4941f31061 to your computer and use it in GitHub Desktop.
# benchmark for http://stackoverflow.com/questions/34511759/remove-superstrings-from-an-array/34511965
require 'benchmark'
source = [ "Rough Collie", "Alsatian", "Standard Poodle", "Poodle", "Collie", "Schnauser", "Border Collie", "Chihuahua" ]
n = 100000
Benchmark.bm do |x|
x.report('delete_if+any') {
n.times{
arr = source.dup
arr.delete_if{|candidate|
arr.any? { |s| candidate != s && candidate.include?(s) }
}
}
}
x.report('uniq+sort+each') {
n.times{
arr = source.dup
arr.uniq!
arr.sort_by!(&:length)
arr.each{|e1| arr.reject!{|e2| e1 != e2 && e2.include?(e1)}}
}
}
end
# ruby 2.2.3p173:
# user system total real
# delete_if+any 1.120000 0.010000 1.130000 ( 1.140818)
# uniq+sort+each 2.010000 0.010000 2.020000 ( 2.051110)
# ruby 2.3.0 :
# user system total real
# delete_if+any 1.180000 0.000000 1.180000 ( 1.197016)
# uniq+sort+each 1.830000 0.020000 1.850000 ( 1.868836)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment