in reply to Re: Re: reversing a sort...
in thread reversing a sort...

He was pointing out that sort {$a cmp $b} @list is slower than sort @list because you save the overhead of calling the sort function. The difference should be big enough at some point that it is faster to reverse sort @list instead of sort {$b cmp $a} @list.

Testing this is going to be tricky, of course, because sorting effiency depends on the order you see elements in.

Also this is a very, very special case. With more complex sorts there is choice about how to write it either way, and reverse has to be a waste of time.

UPDATE
Bloody language maintainers. Going and rendering my hard-won optimization knowledge obsolete by making everything fast. Bah.

Replies are listed 'Best First'.
Re: Re (tilly) 3: reversing a sort...
by chipmunk (Parson) on May 09, 2001 at 05:22 UTC
    Well, that's just no longer the case. In perl5.6, the four common sort routines: { $a cmp $b } { $a <=> $b } { $b cmp $a } { $b <=> $a } are all optimized. This was change 2595 in perl5.005_55 on Jan 13, 1999. (See the Changes file (big!) or the patch.)