Skip to content

Instantly share code, notes, and snippets.

@marcel
Created June 20, 2012 01:24
Show Gist options
  • Save marcel/2957562 to your computer and use it in GitHub Desktop.
Save marcel/2957562 to your computer and use it in GitHub Desktop.
require "rubygems"
require "rbench"
require "set"
RANGE = (1..1000)
ARRAY = RANGE.to_a
SET = Set.new(ARRAY)
WORST_CASE_COMPLEXITY = ARRAY.size + 1
RBench.run(500_000) do
column :one, :title => 'Array'
column :two, :title => 'Set'
column :three, :title => 'Range'
report "Which is fastest?" do
one { ARRAY.include?(WORST_CASE_COMPLEXITY) }
two { SET.include?(WORST_CASE_COMPLEXITY) }
three { RANGE.include?(WORST_CASE_COMPLEXITY) }
end
end
__END__
Array | Set | Range |
-------------------------------------------------------
Which is fastest? 22.470 | 0.192 | 0.303 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment