That’s not really fair to David’s suggestion, though, since in the
benchmark you put the loop around the block creation (so you are really
stressing Proc.new). This seems closer to the original example:
require ‘benchmark’
def a1 n
n.times do
if block_given?
yield
end
end
end
def a2 n, &blk
blk ||= proc{true}
n.times do
blk.call
end
end
Benchmark.bmbm do |x|
x.report(“block_given?”) {
a1(424242) { ‘hi’ }
}
x.report("&blk") {
a2(424242) { ‘hi’ }
}
end
I’m trying to define the missing block given in the context of the
end.compact
slowdown from call is worse than the slowdown from block_given?.
that David.
end
424242.times do
--------------------------------------- total: 8.360000sec
That’s not really fair to David’s suggestion, though, since in the
Well I benchmarked my suggestion, not David’s, probably made another
mistake in my “where to put the post”.
However I felt that they were equivalent, do you agree?
benchmark you put the loop around the block creation (so you are really
stressing Proc.new).
Good point, I was missing that Proc.new was so costly and not #call !
But are we not unfair to #block_given? now?
Actually I tried to benchmark the difference between #call and #yield
only and one can see that
block_given? comes almost for free, as I have shown all too well #proc
is very expensive :(.
Therefore your results are pretty correct although you call
block_given way to often, but as it is so cheap…, did you know
maybe?
Thanks in any case for pointing out my error which was big.
jv - &blk 8.580000 1.400000 9.980000 ( 13.505903)
Rehearsal -----------------------------------------------------
jv - &blk 2.450000 0.010000 2.460000 ( 3.961397)
I was too humble, but that will not happen again
end
…
end
Now of course the proc is a killer as we have as many proces as #call
Hopefully this kind of a reasonable conclusion about the performance
cost of the whole stuff.
I was suggesting that an analysis of how the relative numbers are
changing between 1.8 and 1.9 might be interesting.
Besides the fact that YARV seems quite a bit faster overall, I think
that some of the rules of thumb based on benchmarking on the ‘classic’
Ruby implementation are going to change for the new Ruby.
I was suggesting that an analysis of how the relative numbers are
changing between 1.8 and 1.9 might be interesting.
Besides the fact that YARV seems quite a bit faster overall, I think
that some of the rules of thumb based on benchmarking on the ‘classic’
Ruby implementation are going to change for the new Ruby.
I thought so Rick, but they seem identical, I mean the same factor of
slowfown for proc and call, right?
Just that YARV runs quite faster
So for your (Robert’s) version of the benchmark the difference between
the two approaches has widened in 1.9, while in Joel’s it has
narrowed.
Significant? I don’t know, but interesting.
–
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous