David A. Black said:
Hi --
David A. Black said:
Hi --
On Tue, 7 Dec 2004, Robert Klemme wrote:
13:21:36 [robert.klemme]: ruby yield-vs-block.rb
user system total real
yield 0.250000 0.000000 0.250000 ( 0.249000)
block 0.125000 0.000000 0.125000 ( 0.124000)
13:21:44 [robert.klemme]: cat yield-vs-block.rb
require 'benchmark'
include Benchmark
REP = 100000
def bl_yield(n)
n.times {|i| yield i}
end
def bl_fwd(n,&b)
n.times(&b)
end
Those aren't equivalent, though. The second one would have to be:
n.times {|i| b.call(i) }
to compare the two idioms directly.
Hm, I'm not sure I agree here. My point was to compare performance of two
idioms achieving that a block handed to a method is called from another
method called within that method. You are right with respect to the
invocation chain though.
I think we're talking about different sets of "two idioms",
Yes, we are / were.
but I see
what you mean about your set. As for....
.. there's something nagging in my mind that I can't quite nail down,
having something to do with argument parsing, I think. It's probably
illusory. If I manage to figure out what it is, I'll post it
Thanks! I'm curios to learn what you find out. Wait...
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.
Kind regards
robert