From: MonkeeSage Date: 2008-01-23T17:34:57+09:00 Subject: Re: Is there any way to pass further the "hidden" block? On Jan 14, 6:58 am, Rick DeNatale wrote: > 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. But that didn't seem to make very much difference with the implicit block using the yield keyword, Only with the explicit block passed via formal parameter (see above). That's the reason for my puzzlement -- creating the Proc outside the loop only seems to benefit when explicitly passing it via formal parameter, but not when passing it implicitly using the yield keyword. That seems strange to me. > > 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: Err...I was referring to passing the object "prc" rather than the reference "&prc"...the former being faster than the later. Still curious about the reason for the difference in speed. > 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 Rubyhttp://talklikeaduck.denhaven2.com/ Regards, Jordan