It's even faster if you avoid the sort block, using GRT:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw /timethese cmpthese/;
our @advocates = map {{fld_title => 'Rec' . $_,
fld_type => [qw /National State Local/] -> [ra
+nd 3]}}
1 .. shift || 1000;
our %order = (LOCAL => 'A', STATE => 'B', NATIONAL => 'C');
our %order_r = reverse %order;
our (@limbic, @GRT);
cmpthese -1 => {
limbic => '@limbic = sort {$order {uc $a -> {fld_type}} cmp
$order {uc $b -> {fld_type}} or
$a -> {fld_title} cmp $b -> {fld_titl
+e}}
@advocates',
GRT => '@GRT = map {{fld_title => substr ($_, 1),
fld_type => $order_r {substr $_, 0, 1}}
+}
sort
map {join "", $order {uc $_ -> {fld_type}},
$_ -> {fld_title}} @a
+dvocates',
};
my $limbic = join " " => map {$_ -> {fld_title}} @limbic;
my $GRT = join " " => map {$_ -> {fld_title}} @GRT;
die "Unequal\n" unless $limbic eq $GRT;
__END__
Rate limbic GRT
limbic 61.9/s -- -49%
GRT 121/s 95% --
It does lose the case of fld_type, but that's easily fixable.
Abigail |