Skip to content

Instantly share code, notes, and snippets.

@fcheung
Created May 26, 2015 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcheung/316ae7ac30cd240bd834 to your computer and use it in GitHub Desktop.
Save fcheung/316ae7ac30cd240bd834 to your computer and use it in GitHub Desktop.
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