Last active
July 8, 2019 22:27
-
-
Save nownabe/40f73b5406a80c03af3c12568ce48d3d to your computer and use it in GitHub Desktop.
Ruby block chain performance
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 meth1(&block) | |
meth2(&block) | |
end | |
def meth2 | |
yield | |
end | |
end | |
class B | |
def meth1(&block) | |
meth2(&block) | |
end | |
def meth2(&block) | |
block.call | |
end | |
end | |
class C | |
def meth1 | |
meth2(&Proc.new) | |
end | |
def meth2 | |
yield | |
end | |
end | |
puts "ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE} #{RUBY_REVISION}) [#{RUBY_PLATFORM}]" | |
puts | |
Benchmark.ips do |x| | |
x.report("&block - yield") { A.new.meth1 { 1 } } | |
x.report("&block - block.call") { B.new.meth1 { 1 } } | |
x.report("&Proc.new - yield") { C.new.meth1 { 1 } } | |
x.compare! | |
end | |
__END__ | |
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux] | |
Warming up -------------------------------------- | |
&block - yield 317.786k i/100ms | |
&block - block.call 300.332k i/100ms | |
&Proc.new - yield 212.321k i/100ms | |
Calculating ------------------------------------- | |
&block - yield 6.327M (± 5.6%) i/s - 31.779M in 5.042957s | |
&block - block.call 5.558M (± 4.9%) i/s - 27.931M in 5.039385s | |
&Proc.new - yield 3.140M (± 5.7%) i/s - 15.712M in 5.023740s | |
Comparison: | |
&block - yield: 6327003.6 i/s | |
&block - block.call: 5557820.3 i/s - 1.14x slower | |
&Proc.new - yield: 3139735.8 i/s - 2.02x slower | |
--- | |
ruby 2.5.3p105 (2018-10-18 65156) [x86_64-linux] | |
Warming up -------------------------------------- | |
&block - yield 309.451k i/100ms | |
&block - block.call 176.420k i/100ms | |
&Proc.new - yield 193.108k i/100ms | |
Calculating ------------------------------------- | |
&block - yield 5.780M (± 7.1%) i/s - 28.779M in 5.013362s | |
&block - block.call 2.427M (± 6.7%) i/s - 12.173M in 5.042566s | |
&Proc.new - yield 2.730M (± 5.1%) i/s - 13.711M in 5.036934s | |
Comparison: | |
&block - yield: 5779832.0 i/s | |
&Proc.new - yield: 2729992.8 i/s - 2.12x slower | |
&block - block.call: 2427171.3 i/s - 2.38x slower | |
--- | |
ruby 2.4.1p111 (2017-03-22 58053) [x86_64-linux] | |
Warming up -------------------------------------- | |
&block - yield 185.991k i/100ms | |
&block - block.call 165.535k i/100ms | |
&Proc.new - yield 181.499k i/100ms | |
Calculating ------------------------------------- | |
&block - yield 2.666M (± 5.7%) i/s - 13.391M in 5.041754s | |
&block - block.call 2.391M (± 5.4%) i/s - 11.919M in 5.001634s | |
&Proc.new - yield 2.555M (± 6.8%) i/s - 12.886M in 5.071558s | |
Comparison: | |
&block - yield: 2665830.7 i/s | |
&Proc.new - yield: 2555084.3 i/s - same-ish: difference falls within error | |
&block - block.call: 2390739.4 i/s - same-ish: difference falls within error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment