From: Rick DeNatale Date: 2008-01-14T21:58:08+09:00 Subject: Re: Is there any way to pass further the "hidden" block? On 12/17/07, MonkeeSage wrote: > Hmmm...another thing...passing prc to #explicit_blk as an lval rather > than invoking block_pass shaves another second off the time... > > def explicit_blk(blk) > blk.call("baz") > end > n = 1_000_000 > prc = lambda { | y | y } > Benchmark.bm(10) { | x | > x.report("explicit") { n.times { explicit_blk(prc) } } > } > > # => > user system total real > explicit 5.030000 0.520000 5.550000 ( 5.774020) > > ...so I'm curious about two matters: > > 1.) Why does creating a Proc explicitly and passing it as a block_arg > rather than using a literal block (see previous post) speed up the > #explicit_blk benchmark 2 times, but doesn't have very much effect the > #implicit_blk benchmark? because you've moved the expensive proc creation outside of the benchmark loop. > > 2.) Why is it less expensive to pass a Proc as an lval rather than as > a block_arg? It's not, actually the other way, only slightly: require "benchmark" def explicit_blk(blk) blk.call("baz") end n = 1_000_000 prc = lambda { | y | y } Benchmark.bm(10) { | x | x.report("outside") { n.times { explicit_blk(prc) } } x.report("lval") { n.times { prc = lambda { | y | y }; explicit_blk(prc) } } x.report("parm") { n.times { explicit_blk(lambda { | y | y }) } } } user system total real outside 1.430000 0.010000 1.440000 ( 1.558680) lval 5.460000 0.030000 5.490000 ( 5.885967) parm 5.320000 0.030000 5.350000 ( 5.748286) -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/