public
Last active

  • Download Gist
bench.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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
results.txt
1 2 3 4 5 6 7 8 9 10
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)

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.