Created
May 4, 2018 14:02
-
-
Save dmitriy-kiriyenko/eeaa3d5eb62925c6838b62fc17f01798 to your computer and use it in GitHub Desktop.
Lookup comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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