Re: Sorted Hash



palexvs@xxxxxxxxx wrote:
I filled hash and then printed it (sorted by key):
my %hs = (
key10 => 5,
key5 => b,
aey9 => 7,
)
foreach my $k (sort keys %hs) { print "$k $hs{$k}\n"; }

key - string ([0-9A-F]{72}), 50K records.
How do it more effective?


yu can use the radix sort implementation available from Sort::Key::Radix that is usually faster for this kind of data that the merge sort used internally by perl:


#!/usr/bin/perl

use strict;
use warnings;

use Benchmark qw(cmpthese);
use Sort::Key::Radix qw(ssort);

my @l = ('a'..'z','A'..'Z','1'..'9');

sub genkey { join '', map $l[rand @l], 0..71 }

my @keys = map genkey, 0..50_000;


sub psort { my @sorted = sort @keys }
sub rsort { my @sorted = ssort @keys }

cmpthese (-1, { psort => \&psort,
rsort => \&rsort } );


-----------

psort 4.39/s -- -41%
rsort 7.41/s 69% --
.