AsyncEngine icon indicating copy to clipboard operation
AsyncEngine copied to clipboard

Avoid using &block in methods params

Open ibc opened this issue 13 years ago • 0 comments

Having &block as the last parameter of a method means that a given block is automatically converted into a Proc (which is more than 10 times slower):

def  method_no_block_param
   yield
end

def  method_with_block_param &block
   yield
end


Benchmark.realtime { 1000000.times { method_no_block_param {} } }
=> 0.16652022

Benchmark.realtime { 1000000.times { method_with_block_param {} } }
=> 1.060225561

So I should refactor all the methods by removing the &block param and using îf block_given?.

Also, remember that passing a block to other method does not require converting it into a Proc, just use:

def method1
  yield
end

def method2
  method1 { yield }
end

ibc avatar Sep 06 '12 09:09 ibc