Skip to content

Instantly share code, notes, and snippets.

@nonowarn
Created February 3, 2011 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonowarn/809117 to your computer and use it in GitHub Desktop.
Save nonowarn/809117 to your computer and use it in GitHub Desktop.
require "benchmark"
class Array
def duplicates_reject_uniq
reject { |e| count(e) <= 1 }.uniq
end
def duplicates_uniq_reject
uniq.reject { |e| count(e) <= 1 }
end
end
n = 1000
Benchmark.bm do |bm|
bm.report("reject then uniq") { n.times { ([1] * 100).duplicates_reject_uniq } }
bm.report("uniq then reject") { n.times { ([1] * 100).duplicates_uniq_reject } }
end
# user system total real
# reject then uniq 0.070000 0.000000 0.070000 ( 0.078661)
# uniq then reject 0.020000 0.000000 0.020000 ( 0.013102)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment