Create a gist now

Instantly share code, notes, and snippets.

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
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)
@plukevdh
Owner

Pretty useless w/o also benching runtime of the setup, but interesting all the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment