Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created January 30, 2012 22:37
Show Gist options
  • Save louismullie/1707242 to your computer and use it in GitHub Desktop.
Save louismullie/1707242 to your computer and use it in GitHub Desktop.
Benchmark: Array.include? vs. Hash.include?
require 'benchmark'
array = (1..1_000_000).to_a
hash = Hash[array.map {|x| [x, nil]}]
Benchmark.bm(15) do |x|
x.report("Array.include?") { 1000.times { array.include?(500_000) } }
x.report("Hash.include?") { 1000.times { hash.include?(500_000) } }
end
# user system total real
# Array.include? 46.190000 0.160000 46.350000 ( 46.593477)
# Hash.include? 0.000000 0.000000 0.000000 ( 0.000523)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment