Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Efficiently sorting array by non-alpha strings

by knowmad (Monk)
on Feb 20, 2004 at 16:21 UTC ( [id://330585]=note: print w/replies, xml ) Need Help??


in reply to Re: Efficiently sorting array by non-alpha strings
in thread Efficiently sorting array by non-alpha strings

L~R,

I had not considered the secondary sort by title. Nice touch!

I added your example into my benchmark tests which now include my original hack, Abigail's bucketsort example and yours (which looks to me like a Schwarztian Transform, no?). Yours takes the cake:

Benchmark: timing 500000 iterations of bucketsort, sort, st... bucketsort: 1 wallclock secs ( 1.73 usr + 0.00 sys = 1.73 CPU) @ 28 +9017.34/s (n=500000) sort: 1 wallclock secs ( 0.90 usr + 0.00 sys = 0.90 CPU) @ 55 +5555.56/s (n=500000) st: 0 wallclock secs ( 0.66 usr + 0.00 sys = 0.66 CPU) @ 75 +7575.76/s (n=500000) Rate bucketsort sort st bucketsort 289017/s -- -48% -62% sort 555556/s 92% -- -27% st 757576/s 162% 36% --

Thanks,

-Wm

Replies are listed 'Best First'.
Re: Re: Re: Efficiently sorting array by non-alpha strings
by Limbic~Region (Chancellor) on Feb 20, 2004 at 16:31 UTC
    knowmad,
    Actually, you can avoid the ST all together, which should be even faster.
    #!/usr/bin/perl use strict; use warnings; my @advocates = ( {fld_title => 'Rec1', fld_type => 'National'}, {fld_title => 'Rec2', fld_type => 'State' }, {fld_title => 'Rec3', fld_type => 'Local' } ); my %order = (LOCAL => 'A', STATE => 'B', NATIONAL => 'C'); my @sorted = sort { $order{uc $a->{fld_type}} cmp $order{uc $b->{fld_t +ype}} || $a->{fld_title} cmp $b->{fld_title} } @advocates; print "$_->{fld_title}\n" for @sorted;
    Cheers - L~R
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://330585]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found