OK I found a way to make it faster. The compromise is to trade speed for space... Instead of calling sort for every item that gets added to the list of the select few, I collect $workset extra items before doing the sort... That reduces the total number of calls to a relatively small number, roughly $workset times fewer calls. In my example code, with $workset = 100, I got just 3 calls total.
use strict; use Time::HiRes 'time'; my %HASH; foreach('AA' .. 'ZZ') { $HASH{$_} = 1 + int rand 1000; } my $t0 = time; my @top; { my $keep = 100; my $workset = 100; my $threshold; my $added = 0; while(my($key, $value) = each %HASH) { if(@top < $keep) { printf "Starting with %s => %d\n", $key => $value; push @top, [$key, $value]; } elsif(not defined $threshold or $value > $threshold) { printf "Adding %s => %d\n", $key => $value; push @top, [$key, $value]; if(++$added >= $workset) { printf "Shakedown from %d items\n", scalar @top; @top = sort { $b->[1] <=> $a->[1] } @top; $threshold = $top[my $i = $keep - 1][1]; printf "New threshold: %d\n", $threshold; while($top[++$i][1] == $threshold) { } $#top = $i-1; printf "Keeping %d items\n", scalar @top; $added = 0; } } elsif($value == $threshold) { printf "Adding %s => %d\n", $key => $value; push @top, [$key, $value]; } else { printf "Skipping %s => %d\n", $key => $value; } } printf "Final shakedown from %d items\n", scalar @top; @top = sort { $b->[1] <=> $a->[1] } @top; if(@top > $keep) { $threshold = $top[my $i = $keep - 1][1]; printf "Final threshold: %d\n", $threshold; while($top[++$i][1] == $threshold) { } $#top = $i-1; printf "Keeping %d items\n", scalar @top; } } use Data::Dumper; $Data::Dumper::Terse = 1; print Dumper scalar @top, \@top; my $t1 = time; $, = "\t"; print $t1-$t0;

In reply to Re: Re: Re: *Fastest* way to print a hash sorted by value by bart
in thread *Fastest* way to print a hash sorted by value by smellysocks

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.