Skip to content

Instantly share code, notes, and snippets.

@headius
Last active December 14, 2015 13:58
Show Gist options
  • Select an option

  • Save headius/5097185 to your computer and use it in GitHub Desktop.

Select an option

Save headius/5097185 to your computer and use it in GitHub Desktop.
Benchmark of various method definition forms
require 'benchmark/ips'
# form #1: normal def
module Form1
def self.form1(a, b, c)
a + b + c
end
end
# form #2: class eval string
module Form2
class_eval "
def self.form2(a, b, c)
a + b + c
end
"
end
# form #3: define_method
module Form3
class << self
define_method :form3 do |a, b, c|
a + b + c
end
end
end
# form #4: function object in def
module Form4
Function = Object.new.instance_eval do
def form4(a, b, c)
a + b + c
end
self
end
def self.form4(a, b, c)
Function.form4(a, b, c)
end
end
# form #5: function object in define_method
module Form5
Function = Object.new.instance_eval do
def form5(a, b, c)
a + b + c
end
self
end
class << self
define_method :form5 do |a, b, c|
Function.form5(a, b, c)
end
end
end
# form #6: function object method in def
module Form6
Function = Object.new.instance_eval do
def form6(a, b, c)
a + b + c
end
self
end.method(:form6)
def self.form6(a, b, c)
Function.(a, b, c)
end
end
# form #7: function object method in define_method
module Form7
Function = Object.new.instance_eval do
def form7(a, b, c)
a + b + c
end
self
end.method(:form7)
class << self
define_method :form7 do |a, b, c|
Function.(a, b, c)
end
end
end
# form #8: function object method bound via define method
module Form8
Function = Object.new.instance_eval do
def form8(a, b, c)
a + b + c
end
self
end.method(:form8)
class << self
define_method :form8, &Function
end
end
# form #9: function object unbound method via define method
module Form9
def self.form9_unbound(a, b, c)
a + b + c
end
Function = Form9.method(:form9_unbound).unbind
class << self
define_method :form9, Function
end
end
# form #10: function object bound method via define method
# The bound method form is a prototype in JRuby
module Form10
Function = Object.new.instance_eval do
def form10(a, b, c)
a + b + c
end
self
end.method(:form10)
class << self
define_method :form10, Function
end
end if RUBY_ENGINE == 'jruby'
Benchmark.ips do |bm|
bm.report('control loop') do |n|
x = 0
while n > 0
x += 1 + 2 + n
n-=1
end
x
end
bm.report('#1: normal def') do |n|
x = 0
while n > 0
x += Form1.form1(1, 2, n)
n-=1
end
x
end
bm.report('#2: class_eval') do |n|
x = 0
while n > 0
x += Form2.form2(1, 2, n)
n-=1
end
x
end
bm.report('#3: d_m') do |n|
x = 0
while n > 0
x += Form3.form3(1, 2, n)
n-=1
end
x
end
bm.report('#4: func obj in def') do |n|
x = 0
while n > 0
x += Form4.form4(1, 2, n)
n-=1
end
x
end
bm.report('#5: func obj in d_m') do |n|
x = 0
while n > 0
x += Form5.form5(1, 2, n)
n-=1
end
x
end
bm.report('#6: method in def') do |n|
x = 0
while n > 0
x += Form6.form6(1, 2, n)
n-=1
end
x
end
bm.report('#7: method in d_m') do |n|
x = 0
while n > 0
x += Form7.form7(1, 2, n)
n-=1
end
x
end
bm.report('#8: d_m proc method') do |n|
x = 0
while n > 0
x += Form8.form8(1, 2, n)
n-=1
end
x
end
bm.report('#9: d_m unbound method') do |n|
x = 0
while n > 0
x += Form9.form9(1, 2, n)
n-=1
end
x
end
bm.report('#10: d_m bound method') do |n|
x = 0
while n > 0
x += Form10.form10(1, 2, n)
n-=1
end
x
end if RUBY_ENGINE == 'jruby'
end
ext-jruby-local ~/projects/jruby $ jruby -Xcompile.invokedynamic=true bench_def_forms.rb
Calculating -------------------------------------
control loop 148869 i/100ms
#1: normal def 193617 i/100ms
#2: class_eval 190712 i/100ms
#3: d_m 148644 i/100ms
#4: func obj in def 182998 i/100ms
#5: func obj in d_m 147405 i/100ms
#6: method in def 151611 i/100ms
#7: method in d_m 150678 i/100ms
#8: d_m proc method 144114 i/100ms
#9: d_m unbound method
138141 i/100ms
#10: d_m bound method
188793 i/100ms
-------------------------------------------------
control loop 41566874.7 (±17.5%) i/s - 198888984 in 4.947000s
#1: normal def 32615427.1 (±11.0%) i/s - 159927642 in 4.966000s
#2: class_eval 33271419.9 (±11.6%) i/s - 163249472 in 4.975001s
#3: d_m 6840842.4 (±4.3%) i/s - 34188120 in 5.007000s
#4: func obj in def 32840930.1 (±11.5%) i/s - 161221238 in 4.979000s
#5: func obj in d_m 6667882.5 (±4.4%) i/s - 33313530 in 5.006000s
#6: method in def 15302782.7 (±6.5%) i/s - 75957111 in 4.984999s
#7: method in d_m 6525500.3 (±4.5%) i/s - 32546448 in 4.998000s
#8: d_m proc method 7505013.2 (±4.5%) i/s - 37469640 in 5.003000s
#9: d_m unbound method
13707114.6 (±7.2%) i/s - 68103513 in 4.995000s
#10: d_m bound method
29066839.9 (±10.1%) i/s - 143293887 in 4.986000s
ext-jruby-local ~/projects/jruby $ ruby2.0.0 bench_def_forms.rb
Calculating -------------------------------------
control loop 107413 i/100ms
#1: normal def 101952 i/100ms
#2: class_eval 101509 i/100ms
#3: d_m 95389 i/100ms
#4: func obj in def 93138 i/100ms
#5: func obj in d_m 89368 i/100ms
#6: method in def 84739 i/100ms
#7: method in d_m 83572 i/100ms
#8: d_m proc method 86180 i/100ms
#9: d_m unbound method
104428 i/100ms
-------------------------------------------------
control loop 20735478.7 (±3.8%) i/s - 103438719 in 4.998078s
#1: normal def 12317831.4 (±2.3%) i/s - 61579008 in 5.001797s
#2: class_eval 12193883.6 (±2.7%) i/s - 60905400 in 4.998333s
#3: d_m 7123248.5 (±2.2%) i/s - 35675486 in 5.010758s
#4: func obj in def 7160576.3 (±2.0%) i/s - 35858130 in 5.009823s
#5: func obj in d_m 5539517.2 (±1.9%) i/s - 27704080 in 5.002938s
#6: method in def 5583301.7 (±2.1%) i/s - 27963870 in 5.010727s
#7: method in d_m 4140541.2 (±2.1%) i/s - 20725856 in 5.008020s
#8: d_m proc method 4091973.3 (±4.0%) i/s - 20424660 in 4.999767s
#9: d_m unbound method
11849336.0 (±2.8%) i/s - 59210676 in 5.001007s
ext-jruby-local ~/projects/jruby $ ../rubinius/bin/rbx -X19 bench_def_forms.rb
Calculating -------------------------------------
control loop 155169 i/100ms
#1: normal def 225408 i/100ms
#2: class_eval 226996 i/100ms
#3: d_m 210027 i/100ms
#4: func obj in def 215762 i/100ms
#5: func obj in d_m 202809 i/100ms
#6: method in def 104923 i/100ms
#7: method in d_m 141954 i/100ms
#8: d_m proc method 68823 i/100ms
#9: d_m unbound method
65403 i/100ms
-------------------------------------------------
control loop 120415071.5 (±4.0%) i/s - 588245679 in 4.895947s
#1: normal def 75389989.6 (±2.8%) i/s - 373951872 in 4.964552s
#2: class_eval 74922532.1 (±4.6%) i/s - 371365456 in 4.973634s
#3: d_m 22805307.8 (±2.7%) i/s - 113834634 in 4.995622s
#4: func obj in def 41546356.8 (±2.9%) i/s - 207131520 in 4.989834s
#5: func obj in d_m 18845012.6 (±2.2%) i/s - 94103376 in 4.995948s
#6: method in def 5017986.5 (±2.8%) i/s - 25076597 in 5.001509s
#7: method in d_m 4434300.4 (±2.5%) i/s - 22144824 in 4.997382s
#8: d_m proc method 1203643.0 (±2.8%) i/s - 6056424 in 5.036864s
#9: d_m unbound method
1199799.9 (±1.6%) i/s - 6017076 in 5.016469s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment