Skip to content

Instantly share code, notes, and snippets.

@headius
Created December 29, 2014 20:37
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 headius/d1e6b73c79ca608d6c6c to your computer and use it in GitHub Desktop.
Save headius/d1e6b73c79ca608d6c6c to your computer and use it in GitHub Desktop.
Bench nested super and constant logic
# Test the performance of testing for equality in a class that includes
# a module and calls super on it. See if there is any difference between
# arg == Const and Const == arg for the runtimes.
#
require 'rubygems'
require 'benchmark'
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
module Foo1
def compare_it(val)
A == val ||
B == val
end
end # Foo1
module Foo2
def compare_it(val)
val == A ||
val == B
end
end # Foo2
class Bar1
include Foo1
def compare_it(val)
super ||
C == val ||
D == val ||
E == val ||
F == val ||
G == val ||
H == val ||
I == val
end
end # Bar1
class Bar2
include Foo2
def compare_it(val)
super ||
val == C ||
val == D ||
val == E ||
val == F ||
val == G ||
val == H ||
val == I
end
end # Bar2
@bar1 = Bar1.new
@bar2 = Bar2.new
Iterations = 50_000_000
Benchmark.bmbm("== order comparison for speed".size) do |x|
x.report("bar1 A in super:") do
Iterations.times { @bar1.compare_it(1) }
end
x.report("bar2 A in super:") do
Iterations.times { @bar2.compare_it(1) }
end
x.report("bar1 C:") do
Iterations.times { @bar1.compare_it(3) }
end
x.report("bar2 C:") do
Iterations.times { @bar2.compare_it(3) }
end
x.report("bar1 I:") do
Iterations.times { @bar1.compare_it(9) }
end
x.report("bar2 I:") do
Iterations.times { @bar2.compare_it(9) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment