Skip to content

Instantly share code, notes, and snippets.

@h3h
Created September 5, 2011 00:58
Show Gist options
  • Save h3h/1193812 to your computer and use it in GitHub Desktop.
Save h3h/1193812 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
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