-
-
Save nateberkopec/43cb5cfa95a417b90485 to your computer and use it in GitHub Desktop.
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/ips' | |
require 'set' | |
ARRAY_SIZE = 10 | |
class TestBench | |
def array | |
Array((1..ARRAY_SIZE)) | |
end | |
def set | |
Set.new((1..ARRAY_SIZE)) | |
end | |
def slow | |
array.include?(rand(ARRAY_SIZE)) | |
end | |
def fast | |
set.include?(rand(ARRAY_SIZE)) | |
end | |
end | |
test = TestBench.new | |
Benchmark.ips do |x| | |
x.report("set include") { test.fast } | |
x.report("array include") { test.slow } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment