> >> After I sent this I had a flash of enlightenment:
> >> $max = (sort {$a <=> $b} @_)[-1];
> >> May be slower, though, I don't know.
> >
> > How many times have I seen this? I mean, I've seen
> > this construct many times, and the question deserves
> > a place in the Perl FAQ.
>
> How to find the min/max value of an array is certainly a FAQ, so it
> should be put in the official Perl FAQ. How does someone recommend
> this?
perldoc perlfaq
"How to contribute to this document" ;-)
> I couldn't find the benchmark discussion from a few months ago, so
> I tested the various suggestions already mentioned on the list. I
> used the following max functions:
>
> [Snip code and benchmarks]
>
> I'm a litttle puzzled as to why max2 (foreach with if modifier) is
> consistently about 25% faster than max4 (foreach with ternary operator).
> My guess is that the difference is due to how often the assignment is
> done. With the if modifier, the assignment is done only when necessary;
> with the ternary operator, the assignment is done for every element of
> the array (most of the time uselessly assigning $max = $max).
>
Useless assignment in Perl is costly, like any language. Perl also has to
do reference counting that means it takes a few extra cycles. A more fair
experiment is to benchmark if/else vs. ternary operator where each condition
does the same thing... they should match, otherwise somebody hasn't optimised
it correctly.
Jonathan Paton
Jonathan Paton
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]