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

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;