in reply to Re: Re^2: When the Best Solution Isn't
in thread When the Best Solution Isn't
You know (leaving the in-place assignment issue aside), it occured to me (duh) that this is a general purpose but faster rephrasing of the Schwartzian Transform:
sub sxsorta (&;) { my $sort_key = shift; my @rank = map &$sort_key, @_; @_[ sort { $rank[$a] cmp $rank[$b] } 0 .. $#_ ]; } sub sxsortn (&;) { my $sort_key = shift; my @rank = map &$sort_key, @_; @_[ sort { $rank[$a] <=> $rank[$b] } 0 .. $#_ ]; }
The gains over the regular ST comes from the fact that it doesn't create hundreds of tiny anonymous arrays, and that the sort callback is slightly faster because it only has to do an array lookup but no dereferencing.
The drawback is that with a lexically scoped (as it should be) @rank, there's no way to pass a userspecified callback to the routines without inflicting a massive performance hit. To do more complex sorts, one therefor has to separately spell this idiom out in their own code.
Makeshifts last the longest.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Faster than the speed of sound (was: When the Best Solution Isn't)
by blakem (Monsignor) on Sep 23, 2002 at 17:58 UTC | |
by Aristotle (Chancellor) on Sep 23, 2002 at 18:04 UTC | |
by blakem (Monsignor) on Sep 23, 2002 at 18:12 UTC | |
Re: Faster than the speed of sound (was: When the Best Solution Isn't)
by demerphq (Chancellor) on Apr 16, 2003 at 23:10 UTC | |
by Aristotle (Chancellor) on Apr 18, 2003 at 11:00 UTC |
In Section
Meditations