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