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.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: Sorting a hash of arrays based on a particular array element. by Random_Walk
in thread Sorting a hash of arrays based on a particular array element. by aditya.singh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.