in reply to Re^4: top ten things every Perl hacker should know
in thread top ten things every Perl hacker should know
You'll find time after time again that the straightforward sort block is simpler to write in Perl than the fancy sorts. OK, thinking carefully about it, there is one exception. And that exception is where the code to extract "what you want to sort by" is very complex, so that it is more complex to do it both for $a and $b than it is to do it once and have a Schwartzian Transform. But I don't think I've ever encountered that in real life. (Plus one can just move the complex logic into a function and call the function twice. With anonymous functions one can do it inline, and it will still be simpler than a Schwartzian Transform.)
And, of course, someone who hasn't studied sorting tricks is always going to find the straightforward sort block far easier to read.
However the sort block executes more times than mangle/extract blocks do in the Schwartzian Transform or the Guttman-Rosler Transform. So the more work you move from the sort blocks to mangle/extract, the more time you'll save. The GRT is faster than the Schwartzian Transform because it uses a simpler data structure (a string), and so the sort block can be made even faster (in fact it is the default string compare).
People think that this is cool because they are surprised that this change can have such big performance implications. But it is an optimization, and the code you get is more complex (at least in Perl).
Update: hv noted that I'd written GST instead of GRT. Fixed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: top ten things every Perl hacker should know
by johngg (Canon) on Mar 17, 2006 at 19:42 UTC | |
by tilly (Archbishop) on Mar 17, 2006 at 22:44 UTC | |
by johngg (Canon) on Mar 18, 2006 at 12:07 UTC |