Skip to content

Instantly share code, notes, and snippets.

@schneems
Created June 16, 2016 01:57
Show Gist options
  • Save schneems/925ff1988066d4d582915fdf948ca142 to your computer and use it in GitHub Desktop.
Save schneems/925ff1988066d4d582915fdf948ca142 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'set'
set = Set.new [String, Symbol, Fixnum, Bignum, TrueClass, FalseClass, NilClass]
hash = set.each_with_object({}) do |type, hash|
hash[type] = true
end.freeze
values = ["", :symbol, 1, 10000000000000000000000000000000000, true, false, nil]
values.each do |value|
puts "## value = #{ value.inspect }"
Benchmark.ips do |x|
x.report("set ") {|num|
i = 0
while i < num
set.include?(value.class)
i += 1
end
}
x.report("hash") {|num|
i = 0
while i < num
hash[value.class]
i += 1
end
}
x.report("case") {|num|
i = 0
while i < num
case value
when String, Symbol, Fixnum, Bignum, TrueClass, FalseClass, NilClass
true
when Array, Set
true
when Hash
true
end
i += 1
end
}
x.compare!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment