Created
March 28, 2011 18:37
-
-
Save plukevdh/890996 to your computer and use it in GitHub Desktop.
This file contains 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' | |
keys = [] | |
@uniq_keys = [] | |
def add_key(key) | |
@uniq_keys << key unless @uniq_keys.include?(key) | |
end | |
hasher1 = {} | |
50000.times do |x| | |
srand(x) | |
key = rand(10000).to_s | |
keys << key | |
add_key key | |
hasher1[key] = x; | |
end | |
hasher2 = {} | |
50000.times do |x| | |
srand(x); | |
key = rand(10000).to_s | |
keys << key | |
add_key key | |
hasher2[key] = x; | |
end | |
big = (Array(hasher1) | Array(hasher2)).map(&:first) | |
raise "NotEqual! #{big.count} - #{@uniq_keys.count}" unless big == @uniq_keys | |
raise "NotEqual!" unless keys.uniq == @uniq_keys | |
Benchmark.bmbm do |x| | |
x.report{ (Array(hasher1) | Array(hasher2)).map(&:first) } | |
x.report{ keys.uniq! } | |
x.report{ @uniq_keys } | |
end |
This file contains 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
Rehearsal ------------------------------------ | |
0.210000 0.000000 0.210000 ( 0.213641) | |
0.050000 0.000000 0.050000 ( 0.054077) | |
0.000000 0.000000 0.000000 ( 0.000009) | |
--------------------------- total: 0.260000sec | |
user system total real | |
0.200000 0.000000 0.200000 ( 0.201144) | |
0.010000 0.000000 0.010000 ( 0.006215) | |
0.000000 0.000000 0.000000 ( 0.000011) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty useless w/o also benching runtime of the setup, but interesting all the same.