Skip to content

Instantly share code, notes, and snippets.

@wesmaldonado
Created May 17, 2010 22:06
Show Gist options
  • Save wesmaldonado/404290 to your computer and use it in GitHub Desktop.
Save wesmaldonado/404290 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
require 'benchmark'
require 'set'
array = (1..1_000_000).to_a
hash = {}
array.map {|x| hash[x] = nil}
a_set = array.to_set
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) } }
x.report("Set.include?") { 1000.times { a_set.include?(500_000) } }
end
## user system total real
## Array.include? 38.730000 0.010000 38.740000 ( 38.853568)
## Hash.include? 0.000000 0.000000 0.000000 ( 0.000922)
## Set.include? 0.000000 0.000000 0.000000 ( 0.001652)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment