Skip to content

Instantly share code, notes, and snippets.

@afcapel
Created November 16, 2011 22:49
Show Gist options
  • Save afcapel/1371758 to your computer and use it in GitHub Desktop.
Save afcapel/1371758 to your computer and use it in GitHub Desktop.
Remove duplicates benchmark
require 'benchmark'
ary = 1000.times.collect { rand(2000) }
Benchmark.bm(10) do |x|
x.report { ary.select { |e| ary.count(e) > 1 }.uniq }
x.report { ary.group_by {|e| e}.select { |k, v| v.size > 1 }.map(&:first) }
x.report { ary.uniq.select { |i| ary.count(i) > 1 } }
x.report { ary.uniq.select { |i| ary.delete_at(ary.index(i)) && ary.delete_at(ary.index(i) || ary.length) } }
x.report { ary.uniq.delete_if { |i| ary.one? { |elm| elm == i } } }
end
user system total real
0.050000 0.000000 0.050000 ( 0.050655)
0.000000 0.000000 0.000000 ( 0.001314)
0.030000 0.000000 0.030000 ( 0.037768)
0.020000 0.000000 0.020000 ( 0.017854)
0.000000 0.000000 0.000000 ( 0.000200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment