in reply to Sorting a hash of arrays based on a particular array element.
I read the A Fresh Look at Efficient Perl Sorting paper last week and have been itching to play with packed default sorting so here we go, packed default verses the example code BrowserUK gave above. Sorting two million random records 10 times.
=>cat /tmp/sorttest #!/usr/opt/perl5/bin/perl use strict; use warnings; use Benchmark; srand(42); # the answer my $elements = 2000000; my %hash; for (1..$elements) { my ($key, $one, $two) = rand =~/(\d\.\d{2})(\d{2})(\d{4})/; $hash{$key} = [$one, $two]; } sub packed_default { my @sorted_keys = map substr($_,1), sort map {pack('C', $hash{$_}[0])."$_"} keys %hash; return @sorted_keys; } sub BroweserUK { my @sorted_keys = sort{ $hash{$a}[0] <=> $hash{$b}[0] } keys %hash +; } timethese(10, { 'packed_default' => \&packed_default, 'BroweserUK' => \&BroweserUK, }); __end__ Benchmark: timing 10 iterations of BroweserUK, packed_default... BroweserUK: 0 wallclock secs (0.07 usr + 0.00 sys = 0.07 CPU) @ 142.86/s (n=10) (warning: too few iterations for a reliable count) packed_default: 0 wallclock secs (0.04 usr + 0.00 sys = 0.04 CPU) @ 250.00/s (n=10) (warning: too few iterations for a reliable count)
As they suggest in the paper coercing perl to use the default sort instead of custom sub is almost twice as fast.
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting a hash of arrays based on a particular array element.
by salva (Canon) on Jul 27, 2005 at 18:54 UTC | |
by Random_Walk (Prior) on Jul 28, 2005 at 08:39 UTC | |
by salva (Canon) on Jul 28, 2005 at 10:10 UTC | |
by Random_Walk (Prior) on Jul 28, 2005 at 12:28 UTC | |
by salva (Canon) on Jul 28, 2005 at 13:57 UTC |