Skip to content

Instantly share code, notes, and snippets.

@pjambet
Created April 13, 2021 19:00
Show Gist options
  • Save pjambet/98cb38157147619f0d004b622acd02ae to your computer and use it in GitHub Desktop.
Save pjambet/98cb38157147619f0d004b622acd02ae to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'set'
def bench(size)
Benchmark.ips do |x|
set = Set.new
array = []
hash = {}
size.times do |i|
set.add(i)
array << i
hash[i] = i
end
x.report("Set (#{size})") do
set.include?(1)
set.include?(2)
set.include?(3)
set.include?(0)
set.include?(9999)
end
x.report("Array (#{size})") do
array.include?(1)
array.include?(2)
array.include?(3)
array.include?(0)
array.include?(9999)
end
x.report("Hash (#{size})") do
hash.include?(1)
hash.include?(2)
hash.include?(3)
hash.include?(0)
hash.include?(9999)
end
x.compare!
end
end
bench(3)
bench(1_000_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment