Created
February 3, 2011 06:18
-
-
Save nonowarn/809117 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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