develooper Front page | perl.beginners | Postings from February 2002

Re: finding max value

Thread Previous | Thread Next
February 13, 2002 07:33
Re: finding max value
Message ID:
>>>>> "Jeff" == Jeff 'Japhy' Pinyan <> writes:

Jeff> On Feb 13, Frank said:
>> $max= (sort @values)[-1];

Jeff> You're sorting ASCIIbetically.  You must sort numerically:

Jeff>   (sort { $a <=> $b } @values)[-1];

>> Personally, I'd prefer Japhy's method for efficiency.

Jeff> Yeah, me too. ;)  Sorting to find a min or max is not a good move.

Somebody benchmarked it somewhere recently, and for under 20 or 30
items (I forget the threshold), it was actually faster to *sort* it
than to do the normal high-water-mark algorithm!

Yes, it was counter-intuitive, which is why I remember that. :)

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<> <URL:>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See for onsite and open-enrollment Perl training!

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About