Skip to content

Instantly share code, notes, and snippets.

@tpope
Forked from h3h/to_set_performance.rb
Created September 5, 2011 01:06
Show Gist options
  • Save tpope/1193822 to your computer and use it in GitHub Desktop.
Save tpope/1193822 to your computer and use it in GitHub Desktop.
Don't Convert Arrays to Sets for Inclusion Checks
ids = (1..1_000_000).to_a; nil
x = rand(1_000_000)
Benchmark.realtime do
the_set = ids.to_set
1000.times do
the_set.include?(x)
end
end
# => 0.7543990612030029
Benchmark.realtime do
1000.times do
ids.include?(x)
end
end
# => 44.60349249839783
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment