Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created February 14, 2012 15:48
Show Gist options
  • Save louismullie/1827686 to your computer and use it in GitHub Desktop.
Save louismullie/1827686 to your computer and use it in GitHub Desktop.
Benchmark: Array.include? vs Set.include? vs Hash[]
require 'benchmark'
require 'set'
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
h = {'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1,
'e' => 1, 'f' => 1, 'g' => 1}
s = Set.new(a)
Benchmark.bm do |x|
x.report do
1_000_000.times do
a.include?('d')
end
end
x.report do
1_000_000.times do
h['d']
end
end
x.report do
1_000_000.times do
s.include?('d')
end
end
# user system total real
# 0.520000 0.000000 0.520000 ( 0.521729)
# 0.260000 0.000000 0.260000 ( 0.258423)
# 0.350000 0.000000 0.350000 ( 0.352357)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment