[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Omaha.pm] Sort quickie
Jay Hannah wrote:
> sub sort_by_cap_date ($$) {
> # We have to throw some mojo here since capdate is MMDDYYYY and obviously
> # we can't sort until we turn it into YYYYMMDD... -jhannah 6/14/04
> my ($a, $b) = @_;
> for ($a, $b) {
> s/(\d\d)(\d\d)(\d\d\d\d)/$3$1$2/;
> }
> $a <=> $b;
> }
This sub can be written more efficiently as:
sub by_date2 { substr($a,4) cmp substr($b,4) || $a cmp $b }
or:
sub by_date3 { substr($a,4).$a cmp substr($b,4).$b }
Out of curiosity, I benchmarked 4 different ways to do it:
use strict;
use Benchmark;
my @dates = ( '05012003', '02012004', '11012002',
'05012002', '02012003', '11012001',
'06012002', '05022002', '05022004' ) x 9999;
sub by_date ($$) {
my ($a, $b) = @_;
for ($a, $b) {
s/(\d\d)(\d\d)(\d\d\d\d)/$3$1$2/;
}
$a <=> $b;
}
sub by_date2 { substr($a,4) cmp substr($b,4) || $a cmp $b }
sub by_date3 { substr($a,4).$a cmp substr($b,4).$b }
sub j9 { my @x = sort by_date @dates }
sub j2 { my @x = sort by_date2 @dates }
sub j3 { my @x = sort by_date3 @dates }
sub j1 {
my @x = map { substr($_,4) } sort map { substr($_,4).$_ } @dates
}
timethese(10, {
'j1' => \&j1,
'j2' => \&j2,
'j3' => \&j3,
'j9' => \&j9 });
Results on Linux were:
perl 5.8.4:
j1: 7 wallclock secs ( 7.54 usr + 0.03 sys = 7.57 CPU) @ 1.32/s (n=10)
j2: 10 wallclock secs (10.04 usr + 0.01 sys = 10.05 CPU) @ 1.00/s (n=10)
j3: 13 wallclock secs (13.45 usr + 0.00 sys = 13.45 CPU) @ 0.74/s (n=10)
j9: 169 wallclock secs (168.82 usr + 0.00 sys = 168.82 CPU) @ 0.06/s (n=10)
perl 5.6.1:
j1: 6 wallclock secs ( 6.07 usr + 0.02 sys = 6.09 CPU) @ 1.64/s (n=10)
j2: 5 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 1.87/s (n=10)
j3: 7 wallclock secs ( 7.00 usr + 0.00 sys = 7.00 CPU) @ 1.43/s (n=10)
j9: 75 wallclock secs (74.57 usr + 0.00 sys = 74.57 CPU) @ 0.13/s (n=10)
Which surprised me. I expected j1 to be much faster.
Hugh
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.