Skip to content

Instantly share code, notes, and snippets.

@marshall-lee
Created February 9, 2016 02:04
Show Gist options
  • Select an option

  • Save marshall-lee/85ae3cbc1ccca61355fb to your computer and use it in GitHub Desktop.

Select an option

Save marshall-lee/85ae3cbc1ccca61355fb to your computer and use it in GitHub Desktop.
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}:\n"
require 'benchmark/ips'
def without_block(x,y,z); end
def with_block(x,y,z,&b); end
puts
puts '* no passing a block'
puts
Benchmark.ips do |x|
x.report 'method with block' do
with_block(1,2,3)
end
x.report 'method without block' do
without_block(1,2,3)
end
x.compare!
end
puts
puts '* passing a block'
puts
Benchmark.ips do |x|
x.report 'method with block' do
with_block(1,2,3) { }
end
x.report 'method without block' do
without_block(1,2,3) { }
end
x.compare!
end
# Ruby 2.3.0-p0:
#
# * no passing a block
#
# Calculating -------------------------------------
# method with block 100.240k i/100ms
# method without block 103.667k i/100ms
# -------------------------------------------------
# method with block 5.498M (± 0.4%) i/s - 27.566M
# method without block 6.616M (± 0.8%) i/s - 33.070M
#
# Comparison:
# method without block: 6615605.0 i/s
# method with block: 5497671.8 i/s - 1.20x slower
#
#
# * passing a block
#
# Calculating -------------------------------------
# method with block 73.786k i/100ms
# method without block 104.354k i/100ms
# -------------------------------------------------
# method with block 1.779M (± 6.0%) i/s - 8.854M
# method without block 6.419M (± 2.6%) i/s - 32.141M
#
# Comparison:
# method without block: 6418544.7 i/s
# method with block: 1779275.9 i/s - 3.61x slower
#
#
#
# Ruby 2.2.4-p230:
#
# * no passing a block
#
# Calculating -------------------------------------
# method with block 96.338k i/100ms
# method without block 98.046k i/100ms
# -------------------------------------------------
# method with block 4.099M (± 2.3%) i/s - 20.520M
# method without block 5.280M (± 0.5%) i/s - 26.472M
#
# Comparison:
# method without block: 5279791.5 i/s
# method with block: 4098867.6 i/s - 1.29x slower
#
#
# * passing a block
#
# Calculating -------------------------------------
# method with block 71.199k i/100ms
# method without block 101.587k i/100ms
# -------------------------------------------------
# method with block 1.712M (± 2.7%) i/s - 8.615M
# method without block 5.691M (± 0.4%) i/s - 28.546M
#
# Comparison:
# method without block: 5691093.6 i/s
# method with block: 1711522.5 i/s - 3.33x slower
#
#
#
# Ruby 2.1.8-p440:
#
# * no passing a block
#
# Calculating -------------------------------------
# method with block 101.878k i/100ms
# method without block 103.593k i/100ms
# -------------------------------------------------
# method with block 5.184M (± 1.5%) i/s - 25.979M
# method without block 5.757M (± 0.8%) i/s - 28.799M
#
# Comparison:
# method without block: 5757382.9 i/s
# method with block: 5183517.3 i/s - 1.11x slower
#
#
# * passing a block
#
# Calculating -------------------------------------
# method with block 63.973k i/100ms
# method without block 107.936k i/100ms
# -------------------------------------------------
# method with block 1.253M (± 3.5%) i/s - 6.269M
# method without block 5.512M (± 0.7%) i/s - 27.632M
#
# Comparison:
# method without block: 5511681.9 i/s
# method with block: 1253081.9 i/s - 4.40x slower
#
#
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment