Skip to content

Instantly share code, notes, and snippets.

@colinsurprenant
Created June 26, 2013 17:53
Show Gist options
  • Save colinsurprenant/5869685 to your computer and use it in GitHub Desktop.
Save colinsurprenant/5869685 to your computer and use it in GitHub Desktop.
benchmarks for 3 different ways to dynamically invoke a closure in an instance context
require 'benchmark'
class DSL1
def initialize
@i = 1
end
def self.register(&block)
@block = block
end
def execute(arg)
instance_exec(arg, &self.class.block)
end
def self.block
@block
end
end
class DSL2
def initialize
@i = 1
define_singleton_method(:execute, self.class.block)
end
def self.register(&block)
@block = block
end
def self.block
@block
end
end
class DSL3
def initialize
@i = 1
end
def self.register(&block)
define_method(:execute, block)
end
end
N = 10000000
class Test1 < DSL1
register {|arg| @i + arg}
end
Benchmark.bmbm(30) do |bm|
bm.report("instance_exec") do
N.times do |n|
raise if Test1.new.execute(n) != n + 1
end
end
end
class Test2 < DSL2
register {|arg| @i + arg}
end
Benchmark.bmbm(30) do |bm|
bm.report("define_singleton_method") do
N.times do |n|
raise if Test2.new.execute(n) != n + 1
end
end
end
class Test3 < DSL3
register {|arg| @i + arg}
end
Benchmark.bmbm(30) do |bm|
bm.report("define_method") do
N.times do |n|
raise if Test3.new.execute(n) != n + 1
end
end
end
jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_11-b21 [darwin-x86_64]
Rehearsal ------------------------------------------------------------------
instance_exec 26.490000 1.200000 27.690000 ( 14.887000)
-------------------------------------------------------- total: 27.690000sec
user system total real
instance_exec 22.770000 0.930000 23.700000 ( 13.362000)
Rehearsal ------------------------------------------------------------------
define_singleton_method 32.290000 0.870000 33.160000 ( 22.598000)
-------------------------------------------------------- total: 33.160000sec
user system total real
define_singleton_method 30.920000 0.970000 31.890000 ( 21.751000)
Rehearsal ------------------------------------------------------------------
define_method 2.510000 0.020000 2.530000 ( 2.361000)
--------------------------------------------------------- total: 2.530000sec
user system total real
define_method 2.200000 0.000000 2.200000 ( 2.171000)
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.4.0]
Rehearsal ------------------------------------------------------------------
instance_exec 15.410000 0.400000 15.810000 ( 15.924930)
-------------------------------------------------------- total: 15.810000sec
user system total real
instance_exec 15.500000 0.410000 15.910000 ( 16.045161)
Rehearsal ------------------------------------------------------------------
define_singleton_method 27.170000 0.930000 28.100000 ( 28.352451)
-------------------------------------------------------- total: 28.100000sec
user system total real
define_singleton_method 27.860000 0.950000 28.810000 ( 29.072435)
Rehearsal ------------------------------------------------------------------
define_method 3.760000 0.000000 3.760000 ( 3.805917)
--------------------------------------------------------- total: 3.760000sec
user system total real
define_method 3.720000 0.010000 3.730000 ( 3.752677)
Rehearsal ------------------------------------------------------------------
define_method 4.840000 0.000000 4.840000 ( 4.878589)
--------------------------------------------------------- total: 4.840000sec
user system total real
define_method 4.290000 0.010000 4.300000 ( 4.317462)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment