in reply to Re^3: What are the other option for sorting the keys of hash of hashes
in thread What are the other option for sorting the keys of hash of hashes

No, this is quite efficient. Hash accesses are very fast ... You really should test whether you get any speedup through this

Here is some benchmark code that explores this.

use strict; use warnings; use Benchmark q{cmpthese}; my %hash = map { $_ => { Size => int rand 10000 } } q{aaa} .. q{dzz}; my $rcJethro = sub { my @unsort = map qq{$hash{ $_ }->{ Size }:$_}, keys %hash; my @sort = do { no warnings q{numeric}; sort { $a <=> $b } @unsort; }; foreach ( @sort ) { s{[^:]*:}{}; } }; my $rcSimple = sub { my @sortedKeys = sort { $hash{ $a }->{ Size } <=> $hash{ $b }->{ Size } } keys %hash; }; my $rcClassicST = sub { my @sortedKeys = map { $_->[ 0 ] } sort { $a->[ 1 ] <=> $b->[ 1 ] } map { [ $_, $hash{ $_ }->{ Size } ] } keys %hash; }; my $rcClassicGRT = sub { my @sortedKeys = map { substr $_, 4 } sort map { pack q{NA*}, $hash{ $_ }->{ Size }, $_ } keys %hash; }; cmpthese( -5, { ClassicGRT => $rcClassicGRT, ClassicST => $rcClassicST, Jethro => $rcJethro, Simple => $rcSimple } );

The output.

Rate Simple ClassicST Jethro ClassicGRT Simple 11.3/s -- -13% -46% -55% ClassicST 13.0/s 16% -- -37% -48% Jethro 20.8/s 84% 59% -- -17% ClassicGRT 25.0/s 122% 92% 20% --

Your sorting code is a bit odd in that it appears to be a halfway house between a ST and a GRT. It has some issues with "numeric" warnings and a reliance on Perl doing the right thing (which is does at the moment but perhaps that might change) when doing a numerical sort on non-numerical strings that happen to have leading digits. It is pretty quick though, only beaten by the GRT. I have altered your code a bit by using simple interpolation in the map instead of concatenation and by localising and better targetting the no warnings instruction.

The usual caveats apply regarding my benchmarks. I've cocked them up before and I'm sure I will again.

Cheers,

JohnGG

Update: Corrected typo, s/you/your/

Replies are listed 'Best First'.
Re^5: What are the other option for sorting the keys of hash of hashes
by salva (Canon) on Sep 29, 2008 at 16:30 UTC
    The GRT code you are using packs the numbers as 32 bits which is not a good thing to do for file sizes nowadays!

    Anyway, I have added code to benchmark the sorting algorithms provided by Sort::Key and Sort::Key::Radix...

    and these are the results:
    Rate Simple ClassicST ClassicGRT Jethro SK + SKR Simple 3.08/s -- -45% -69% -70% -78% + -80% ClassicST 5.64/s 83% -- -42% -45% -60% + -63% ClassicGRT 9.79/s 218% 74% -- -5% -31% + -36% Jethro 10.3/s 234% 83% 5% -- -27% + -32% SK 14.1/s 359% 151% 44% 37% -- + -7% SKR 15.2/s 394% 170% 55% 48% 8% + --
Re^5: What are the other option for sorting the keys of hash of hashes
by jethro (Monsignor) on Sep 29, 2008 at 16:21 UTC

    The same reliance on doing the right thing caveat has to be applied to ClassicGRT as well. If at some point perls string comparision puts Umlauts at their right place, that sort will fail.

    Also I'm not sure what happens in perl even now when the string is an utf8 string and the number would be a multi-byte character in utf8