From: Robert Dober Date: 2007-03-03T03:53:07+09:00 Subject: Re: default passed block for method On 3/2/07, Rick DeNatale wrote: > Just for giggles, I combined Robert and Joel's benchmarks and ran them > on both ruby 1.8 and a quite recent 1.9. Perhaps the results are of > interest. > > rick@frodo:/public/rubyscripts$ ruby1.8 blkgiven.rb > Rehearsal ----------------------------------------------------- > rd - block_given? 6.070000 1.620000 7.690000 ( 15.323920) > jv - block_given? 3.530000 0.530000 4.060000 ( 9.952521) > rd - &blk 29.620000 2.090000 31.710000 ( 55.018634) > jv - &blk 8.580000 1.400000 9.980000 ( 13.505903) > ------------------------------------------- total: 53.440000sec > > user system total real > rd - block_given? 6.430000 1.550000 7.980000 ( 12.312925) > jv - block_given? 3.470000 0.630000 4.100000 ( 7.956797) > rd - &blk 28.920000 2.040000 30.960000 ( 55.456023) > jv - &blk 8.860000 1.350000 10.210000 ( 12.264554) > > > rick@frodo:/public/rubyscripts$ ruby1.9 blkgiven.rb > Rehearsal ----------------------------------------------------- > rd - block_given? 1.950000 0.000000 1.950000 ( 2.503581) > jv - block_given? 1.930000 0.010000 1.940000 ( 3.029174) > rd - &blk 10.870000 0.020000 10.890000 ( 21.230573) > jv - &blk 2.430000 0.000000 2.430000 ( 6.296742) > ------------------------------------------- total: 17.210000sec > > user system total real > rd - block_given? 2.040000 0.010000 2.050000 ( 3.862906) > jv - block_given? 1.770000 0.000000 1.770000 ( 2.239562) > rd - &blk 10.520000 0.050000 10.570000 ( 17.941764) > jv - &blk 2.450000 0.010000 2.460000 ( 3.961397) > rick@frodo:/public/rubyscripts$ > --- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > > Rick I think as a matter of fact that both benchmarks are important. I was too humble, but that will not happen again ;) Let us look at Jo�l's first, it applies to the following pattern: def method &blk blk ||= proc{true} MANY.times do ... blk.call ... end end from the performance POV you can get away with it. ================================================== my naive benchmark was benchmarking the following pattern - which exits quite often too. def method &blk blk ||= proc{ true } ... blk.call ... end Now of course the proc is a killer as we have as many proces as #call :( Hopefully this kind of a reasonable conclusion about the performance cost of the whole stuff. Cheers Robert -- We have not succeeded in answering all of our questions. In fact, in some ways, we are more confused than ever. But we feel we are confused on a higher level and about more important things. -Anonymous