Because sort has to find the order of all the elements, not just find the largest one. So if you've got 1000 items, it's spending all its time putting the 999 you don't care about in the right order. (Actually, it takes O(n log n) to sort a list, but only O(n) to find the maximal element using a simple scan.)You should never resort to sorting to find the max or min value of a list unless the side effect of sorting has a benefit later on.Thanks so much for those valuable insights. I am intrigued with your statement above. I wonder why it is so?
Well, yes, but if you specify a block to sort by, it has to go back into perl to call that on every pair of elements. IIRC, there are a few blocks that have been hardcoded ({$a <=> $b}, $b cmp $a, etc), but if all you care about is a single value, there's no point in sorting everything to get it.... when possible, uses a C implementation that makes it really fastI thought the built in sort function "is" implememented under "C" already? right?
In reply to Re^3: Getting Max Value from Different Hashes
by Eimi Metamorphoumai
in thread Getting Max Value from Different Hashes
by neversaint
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |