Skip to content

Instantly share code, notes, and snippets.

@abargnesi
Created May 18, 2016 15:38
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 abargnesi/7e6339e15e04c0a6e6890c35fc702c47 to your computer and use it in GitHub Desktop.
Save abargnesi/7e6339e15e04c0a6e6890c35fc702c47 to your computer and use it in GitHub Desktop.
Benchmark (ips) comparison of value lookup.
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