Re: efficient max() function from sort



Chas. Owens wrote:

On Wed, May 28, 2008 at 5:07 PM, Rob Dixon <rob.dixon@xxxxxxx> 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
.



Relevant Pages

  • Re: Agile/XP book now freely available
    ... Writing code at the ... > design and bad implementation. ... size fits all" in the methodology department. ...
    (comp.lang.java.programmer)
  • Re: Reading 2 array lists
    ... Owens, not Owen. ... you are using Strawberry Perl or Cygwin Perl, ... Pragmas are pragmatic instructions to the Perl. ...
    (perl.beginners)
  • Re: efficient max() function from sort
    ... is slightly faster than using. ... for loop) is preferable to the sort. ...
    (perl.beginners)
  • Re: Is this code totaly a shit2?
    ... could you stop posting posts like this? ... You're seeing the result of your insistence on writing code ...
    (comp.lang.c)
  • Re: Question about style
    ... expression is used for numerical computations since my early BASIC ... inserted the parentheses, ... when writing code, for the benefit of people reading your code, but at ...
    (comp.lang.c)