At 11:41 AM 8/6/01 -0400, Chris Rogers wrote:
>If your data is already in the format shown below as yearmonthday, then a
>standard sort would do just fine:
>
>@test =(20010327, 20001113, 20011225, 19991231, 20100115);
>foreach (sort @test)

Doh, I should have realized that this data also sorts asciibetically as 
well as numerically, so I could have left the '{ $a <=> $b }' out of my 
posting.

Compelled to redeem myself, I decided to see whether the numeric sort might 
have an advantage, i.e., performance.  Fortunately, it does:

use Benchmark 'cmpthese';
my (@unsorted, @sorted);
for (1 .. 200000) {
   push @unsorted, sprintf "%04d%02d%02d", rand(10000), rand(100), rand(100);
}
cmpthese (10 =>
            { ascii   => sub { @sorted = sort               @unsorted },
              numeric => sub { @sorted = sort { $a <=> $b } @unsorted }
            });

Benchmark: timing 10 iterations of alpha, numeric...
      ascii: 85 wallclock secs (84.36 usr +  0.27 sys = 84.63 CPU) 
@  0.12/s (n=10)
    numeric: 80 wallclock secs (79.70 usr +  0.25 sys = 79.95 CPU) 
@  0.13/s (n=10)
         s/iter   alpha numeric
ascii     8.46      --     -6%
numeric   8.00      6%      --

Thin justification, I know :-)
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to