Skip to content

Instantly share code, notes, and snippets.

@dmitriy-kiriyenko
Created May 4, 2018 14:02
Show Gist options
  • Save dmitriy-kiriyenko/eeaa3d5eb62925c6838b62fc17f01798 to your computer and use it in GitHub Desktop.
Save dmitriy-kiriyenko/eeaa3d5eb62925c6838b62fc17f01798 to your computer and use it in GitHub Desktop.
Lookup comparison
require 'benchmark/ips'
require 'set'
array = ('a'..'z').map(&:to_sym)
hash = Hash[array.map {|x| [x, 1]}]
set = Set.new(array)
Benchmark.ips do |x|
x.report('array') { array.include?(:a) }
x.report('hash') { hash.key?(:a) }
x.report('set') { set.include?(:a) }
end
Warming up --------------------------------------
array 268.956k i/100ms
hash 274.570k i/100ms
set 258.043k i/100ms
Calculating -------------------------------------
array 8.310M (± 5.1%) i/s - 41.688M in 5.031376s
hash 8.497M (± 5.7%) i/s - 42.558M in 5.027836s
set 7.785M (± 6.4%) i/s - 38.964M in 5.028996s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment