-
-
Save jgaskins/f6a2ce2d3ee6262c933adc6968ba5178 to your computer and use it in GitHub Desktop.
Comparing Array#includes? vs Set#includes?
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
require "benchmark" | |
small = 1..3 | |
large = 1..1_000_000 | |
small_array = small.to_a | |
small_set = small.to_set | |
large_array = large.to_a | |
large_set = large.to_set | |
result = nil | |
Benchmark.ips do |x| | |
x.report("small array hit") { result = small_array.includes? 2 } | |
x.report("small array miss") { result = small_array.includes? 2 } | |
x.report("large array hit") { result = large_array.includes? 500_000 } | |
x.report("large array miss") { result = large_array.includes? 1_000_001 } | |
x.report("small set hit") { result = small_set.includes? 2 } | |
x.report("small set miss") { result = small_set.includes? 2 } | |
x.report("large set hit") { result = large_set.includes? 500_000 } | |
x.report("large set miss") { result = large_set.includes? 1_000_001 } | |
end | |
pp result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment