Last active
June 1, 2018 16:09
-
-
Save adamstrickland/547867dc43d246acb51f6d197c67054e to your computer and use it in GitHub Desktop.
Benchmark for various ways of doing `if condition1 || condition2` (needs `benchmark-ips` installed)
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" | |
require "benchmark/ips" | |
thing = :baz | |
puts "2 conditions, neither is true" | |
Benchmark.ips do |x| | |
x.report("conditional") do | |
if thing == :foo || thing == :bar | |
true | |
else | |
false | |
end | |
end | |
x.report("case") do | |
case thing | |
when :foo, :bar then true | |
else false | |
end | |
end | |
x.report("any?") do | |
[:foo, :bar].any?{ |s| thing == s } | |
end | |
x.report("include?") do | |
[:foo, :bar].include?(thing) | |
end | |
conditions = [:foo, :bar].freeze | |
x.report("static any?") do | |
conditions.any?{ |s| thing == s} | |
end | |
x.report("static include?") do | |
conditions.include?(thing) | |
end | |
x.compare! | |
end | |
thing = :foo | |
puts "2 conditions, 1st is true" | |
Benchmark.ips do |x| | |
x.report("conditional") do | |
if thing == :foo || thing == :bar | |
true | |
else | |
false | |
end | |
end | |
x.report("case") do | |
case thing | |
when :foo, :bar then true | |
else false | |
end | |
end | |
x.report("any?") do | |
[:foo, :bar].any?{ |s| thing == s } | |
end | |
x.report("include?") do | |
[:foo, :bar].include?(thing) | |
end | |
conditions = [:foo, :bar].freeze | |
x.report("static any?") do | |
conditions.any?{ |s| thing == s} | |
end | |
x.report("static include?") do | |
conditions.include?(thing) | |
end | |
x.compare! | |
end | |
thing = :bar | |
puts "2 conditions, 2nd is true" | |
Benchmark.ips do |x| | |
x.report("conditional") do | |
if thing == :foo || thing == :bar | |
true | |
else | |
false | |
end | |
end | |
x.report("case") do | |
case thing | |
when :foo, :bar then true | |
else false | |
end | |
end | |
x.report("any?") do | |
[:foo, :bar].any?{ |s| thing == s } | |
end | |
x.report("include?") do | |
[:foo, :bar].include?(thing) | |
end | |
conditions = [:foo, :bar].freeze | |
x.report("static any?") do | |
conditions.any?{ |s| thing == s} | |
end | |
x.report("static include?") do | |
conditions.include?(thing) | |
end | |
x.compare! | |
end | |
thing = :bar | |
puts "10 conditions, last is true" | |
Benchmark.ips do |x| | |
x.report("conditional") do | |
if thing == :foo || | |
thing == :quux || | |
thing == :quuux || | |
thing == :quuuux || | |
thing == :quuuuux || | |
thing == :quuuuuux || | |
thing == :quuuuuuux || | |
thing == :quuuuuuuux || | |
thing == :quuuuuuuuux || | |
thing == :bar | |
true | |
else | |
false | |
end | |
end | |
x.report("case") do | |
case thing | |
when :foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar then true | |
else false | |
end | |
end | |
x.report("any?") do | |
[:foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar].any?{ |s| thing == s } | |
end | |
x.report("include?") do | |
[:foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar].include?(thing) | |
end | |
conditions = [:foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar].freeze | |
x.report("static any?") do | |
conditions.any?{ |s| thing == s} | |
end | |
x.report("static include?") do | |
conditions.include?(thing) | |
end | |
x.compare! | |
end | |
thing = :baz | |
puts "10 conditions, none is true" | |
Benchmark.ips do |x| | |
x.report("conditional") do | |
if thing == :foo || | |
thing == :quux || | |
thing == :quuux || | |
thing == :quuuux || | |
thing == :quuuuux || | |
thing == :quuuuuux || | |
thing == :quuuuuuux || | |
thing == :quuuuuuuux || | |
thing == :quuuuuuuuux || | |
thing == :bar | |
true | |
else | |
false | |
end | |
end | |
x.report("case") do | |
case thing | |
when :foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar then true | |
else false | |
end | |
end | |
x.report("any?") do | |
[:foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar].any?{ |s| thing == s } | |
end | |
x.report("include?") do | |
[:foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar].include?(thing) | |
end | |
conditions = [:foo, :quux, :quuux, :quuuux, :quuuuux, :quuuuuux, :quuuuuuux, :quuuuuuuux, :quuuuuuuuux, :bar].freeze | |
x.report("static any?") do | |
conditions.any?{ |s| thing == s} | |
end | |
x.report("static include?") do | |
conditions.include?(thing) | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment