Front page | perl.beginners |
Postings from February 2002
Re: finding max value
Thread Previous
|
Thread Next
From:
merlyn
Date:
February 13, 2002 07:33
Subject:
Re: finding max value
Message ID:
m1u1sls8fg.fsf@halfdome.holdit.com
>>>>> "Jeff" == Jeff 'Japhy' Pinyan <jeffp@crusoe.net> 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
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Thread Previous
|
Thread Next