Created
May 18, 2016 15:38
-
-
Save abargnesi/7e6339e15e04c0a6e6890c35fc702c47 to your computer and use it in GitHub Desktop.
Benchmark (ips) comparison of value lookup.
This file contains 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' | |
numbers = (0..10_000_000).to_a | |
hash = Hash[numbers.map { |n| [n, true] }] | |
set = Set.new(numbers) | |
Benchmark.ips do |x| | |
x.warmup = 5 | |
x.time = 30 | |
x.report('array include?') do | |
numbers.include?(10_000_000) | |
end | |
x.report('hash, key?') do | |
hash.key?(10_000_000) | |
end | |
x.report('hash, dereference value') do | |
hash[10_000_000] | |
end | |
x.report('set lookup') do | |
set.include?(10_000_000) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment