Skip to content

Instantly share code, notes, and snippets.

@fcheung
Created May 26, 2015
Embed
What would you like to do?
set vs hash
require 'benchmark/ips'
require 'set'
set = Set[:$or, :$and, :$nor]
array = %i($or $and $nor)
Benchmark.ips do |x|
x.config(:time => 10, :warmup => 2)
x.report('set') do |times|
i = 0
while i < times
set.include? :$nor
set.include? :$other
i+= 1
end
end
x.report('array') do |times|
i = 0
while i < times
array.include? :$nor
array.include? :$other
i+= 1
end
end
x.compare!
end
Calculating -------------------------------------
set 130.162k i/100ms
array 133.122k i/100ms
-------------------------------------------------
set 7.128M (± 5.3%) i/s - 71.068M
array 7.891M (± 5.0%) i/s - 78.675M
Comparison:
array: 7890818.8 i/s
set: 7128395.4 i/s - 1.11x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment