Re: Sorted Hash
- From: Salvador Fandino <sfandino@xxxxxxxxx>
- Date: Fri, 30 Nov 2007 12:19:03 +0100
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% --
.
- Follow-Ups:
- Re: Sorted Hash
- From: A. Sinan Unur
- Re: Sorted Hash
- References:
- Sorted Hash
- From: palexvs@xxxxxxxxx
- Sorted Hash
- Prev by Date: Re: How to persist and query a hash lookup in memory with Perl ?
- Next by Date: Re: Sorted Hash
- Previous by thread: Re: Sorted Hash
- Next by thread: Re: Sorted Hash
- Index(es):