At 13:29 27/03/2001 +0000, Matthew Byng-Maddick wrote:
> > my @th=(qw(th st nd rd),("th")x16)x2; $th[31]="st";
>
>That's an evil and gross hack.
[snip]
>sub th{(($_[0]-10-$_[0]%10)/10%10)?(qw(th st nd rd),('th')x6)[$_[0]%10]:"th"}
The first one I understood. Not sure about the second but I'll work it out ;-)
I thought I would play around with Benchmark.pm, because I don't use it
nearly often enough, so I made this script:
#! /usr/bin/perl -w
use strict;
use Benchmark;
use POSIX 'strftime';
use vars qw(@th);
@th=(qw(th st nd rd),("th")x16)x2; $th[31]="st";
sub th{(($_[0]-10-$_[0]%10)/10%10)?(qw(th st nd rd),('th')x6)[$_[0]%10]:"th"}
my $count=100000;
timethese($count, {
'Array' => '{
my @time=localtime;
my $dummy = strftime("%e$th[$time[3]] %b %Y\n",
@time);
}',
'Sub' => '{
my @time=localtime;
my $th=th($time[3]);
my $dummy = strftime("%e$th %b %Y\n", @time);
}'
});
Now - I don't know if I've used this right at all - comments and criticisms
gladly accepted.
The output is:
Benchmark: timing 100000 iterations of Array, Sub...
Array: 3 wallclock secs ( 3.33 usr + 0.09 sys = 3.42 CPU)
Sub: 6 wallclock secs ( 5.27 usr + 0.06 sys = 5.33 CPU)
So - Did I get this heinously wrong or is MBM's sub really a lot slower ?
Simon.