Created
February 27, 2017 03:10
-
-
Save natezzz/41961d810226011a0fc6711ecca0c13e to your computer and use it in GitHub Desktop.
Method Seal benchmark
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/ips' | |
class A | |
def foo | |
#puts "foo" | |
2 + 2 | |
end | |
def bar | |
# puts "bar" | |
3 + 3 | |
end | |
end | |
module M | |
refine A do | |
def foo | |
# puts "foo M" | |
4+4 | |
end | |
end | |
end | |
module M2 | |
refine A do | |
def bar | |
#puts "bar M2" | |
5+5 | |
end | |
end | |
end | |
module N | |
refine A do | |
def foo | |
#puts "foo N" | |
6+6 | |
end | |
end | |
end | |
class B | |
def initialize | |
@a = A.new | |
@d = D.new | |
end | |
def foo | |
@a.foo | |
end | |
def foo_d | |
@d.foo | |
end | |
def bar | |
@a.bar | |
end | |
end | |
class C | |
def initialize | |
@a = A.new | |
@d = D.new | |
end | |
def foo | |
@a.foo | |
end | |
def foo_d | |
@d.foo | |
end | |
def bar | |
@a.bar | |
end | |
def d_lambda | |
@d.lambda { @a.foo } | |
end | |
def d_foo | |
@d.foo | |
end | |
def b_foo | |
@b.foo | |
end | |
end | |
class D | |
def initialize | |
@a = A.new | |
end | |
def foo | |
@a.foo | |
end | |
def lambda(&p) | |
p.call | |
end | |
end | |
Benchmark.ips do |x| | |
x.warmup = 2 | |
GC.disable | |
b = B.new | |
c = C.new | |
if ENV['RBENV_VERSION'] == "nate-bit-tracking" | |
guarded [A, B, C] | |
end | |
x.report('No seals') { | |
b.foo # expect "foo" => "foo" | |
b.foo_d | |
b.bar # expect "bar" => "bar" | |
c.foo # expect "foo" => "foo" | |
c.foo_d | |
c.bar # expect "bar" => "bar" | |
} | |
if ENV['RBENV_VERSION'] == "nate-bit-tracking" | |
using M, [B, A] | |
using M2, [B, A] | |
else | |
using M, [B, A, D] | |
using M2, [B, A, D] | |
end | |
x.report('One') { | |
b.foo # expect "foo M" => "foo M" | |
b.foo_d # expect "foo M" | |
b.bar # expect "bar M2" => "bar M2" | |
c.foo # expect "foo" => "foo" | |
c.foo_d # expect "foo" | |
c.bar # expect "bar" => "bar" | |
} | |
if ENV['RBENV_VERSION'] == "nate-bit-tracking" | |
using N, [C, A] | |
else | |
using N, [C, A, D] | |
end | |
x.report('Two') { | |
b.foo # expect "foo M" | |
b.foo_d | |
b.bar # expect "bar M2" | |
c.foo # expect "foo N" | |
c.foo_d | |
c.bar # expect "bar" | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment