From: Robert Klemme Date: 2004-12-08T23:22:30+09:00 Subject: Re: Using yield "David A. Black" schrieb im Newsbeitrag news:Pine.LNX.4.61.0412080603510.9910@wobblini... > On Wed, 8 Dec 2004, Robert Klemme wrote: > > > If the other method (Fixnum#times in my example) would do different > > yielding. Hm... I think I should redefine my set: > > > > 14:52:25 [09_Public]: ruby yield-vs-block.rb > > user system total real > > yield 0.265000 0.000000 0.265000 ( 0.263000) > > yield_star 0.766000 0.000000 0.766000 ( 0.778000) > > block 0.109000 0.000000 0.109000 ( 0.124000) > > 14:52:58 [09_Public]: cat yield-vs-block.rb > > > > require 'benchmark' > > include Benchmark > > > > REP = 100000 > > > > def bl_yield(n) > > n.times {|i| yield i} > > end > > > > def bl_yield_star(n) > > n.times {|*i| yield *i} > > end > > > > def bl_fwd(n,&b) > > n.times(&b) > > end > > > > bm(20) do |b| > > b.report("yield") do > > bl_yield(REP) {|i| i + 1} > > end > > > > b.report("yield_star") do > > bl_yield_star(REP) {|i| i + 1} > > end > > > > b.report("block") do > > bl_fwd(REP) {|i| i + 1} > > end > > end > > > > Uh, even worse performance for "yield *i". *shudder* But not really > > unexpected. One more reason to use &b for forwarding. > > I think my vague concern about argument parsing has to do with the > question of who gets to decide. There might be a case where for some > reason you wanted to do: > > def some_method(n) > n.times {|*i| yield i} > end > > or something weird like that... but I guess that takes us away from > exact equivalence with &b, since now I'm not just passing the args to > yield one at a time as they come. Yeah, the &equivalent would be def some_method(n,&b) n.times {|*i| b.call(i)} end which suffers a similar detour as the yield approach. I think the most common case for "block forwarding" does not involve argument mangling so this is probably a special case. Kind regards robert