Created
September 5, 2011 00:58
-
-
Save h3h/1193812 to your computer and use it in GitHub Desktop.
Don't Convert Arrays to Sets for Inclusion Checks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ids = (1..1_000_000).to_a; nil | |
x = rand(1_000_000) | |
Benchmark.realtime do | |
10.times do | |
ids.to_set.include?(x) | |
end | |
end | |
# => 14.3837828636169 | |
Benchmark.realtime do | |
the_set = ids.to_set | |
10.times do | |
the_set.include?(x) | |
end | |
end | |
# => 1.35075497627258 | |
Benchmark.realtime do | |
10.times do | |
ids.include?(x) | |
end | |
end | |
# => 0.142719984054565 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment