Re: efficient max() function from sort

[email protected] (Rob Dixon)
Newsgroups perl.beginners
Message-ID <[email protected]>
Chas. Owens wrote:
>
> On Wed, May 28, 2008 at 5:07 PM, Rob Dixon <[email protected]> wrote:
> snip
>>  $max = (sort {$a <=> $b} @z)[-1];
> snip
> 
> my $max = (sort { $b <=> $a } @z)[0];
> 
> is slightly faster than using [-1].
> 
> You should only use this form if performance matters and you know that
> @z is smaller than 250 items (over 250 items List::Util::max is
> faster).

But in general the bottleneck is the programmer. Whether we are writing code
purely for ourselves or for commercial purposes it is always more efficient in
every sense to write something that we recognize rather than something that will
process faster.

>   use List::Util qw/max/;
>   $max = max @z;

is always the best option, unless very few people are writing code for many
millions of others to use, or the circumstances are otherwise extraordinary.

Rob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.