From: "David A. Black" Date: 2004-12-08T23:12:56+09:00 Subject: Re: Using yield 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. David -- David A. Black dblack@wobblini.net