Skip to content

Instantly share code, notes, and snippets.

@nateberkopec
Last active December 15, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nateberkopec/43cb5cfa95a417b90485 to your computer and use it in GitHub Desktop.
Save nateberkopec/43cb5cfa95a417b90485 to your computer and use it in GitHub Desktop.
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