in reply to Re: Sort of anonymous hash
in thread Sort of anonymous hash
To expand on ikegami's response, using a Schwartzian Transform only makes sense when the item(s) by which to sort is/are obtained by some expensive transformation. That transformation gets repeated many times for each element being sorted as the sort algorith moves it into the right place by successive comparisons with other elements. There was no transformation required in your code as $hash{$_}{desc} could be accessed directly.
Consider the following two code snippets that sort files by modification time (requiring an lstat call, our expensive transformation). Note the timings; using Benchmark would be better but this was just a quick example.
knoppix@Knoppix:~$ time perl -Mstrict -wle ' -> my $d = q{/usr/bin}; -> my @fns = do { opendir my $dh, $d or die $!; readdir $dh }; -> my @sorted = -> sort { ( lstat qq{$d/$a} )[ 9 ] <=> ( lstat qq{$d/$b} )[ 9 ] } -> @fns;' real 0m1.614s user 0m0.307s sys 0m1.273s knoppix@Knoppix:~$ time perl -Mstrict -wle ' -> my $d = q{/usr/bin}; -> my @fns = do { opendir my $dh, $d or die $!; readdir $dh }; -> my @sorted = -> map { $_->[ 0 ] } -> sort { $a->[ 1 ] <=> $b->[ 1 ] } -> map { [ $_, lstat qq{$d/$_} ] } -> @fns;' real 0m0.281s user 0m0.100s sys 0m0.160s knoppix@Knoppix:~$
I hope this is of interest.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Sort of anonymous hash
by larsss31 (Acolyte) on Sep 30, 2009 at 08:12 UTC | |
by ikegami (Patriarch) on Sep 30, 2009 at 10:31 UTC | |
by larsss31 (Acolyte) on Sep 30, 2009 at 11:12 UTC |