Hello Brett,
Tuesday, July 03, 2001, Brett W. McCoy <[EMAIL PROTECTED]> wrote:
>> > > > Unpack works well with fixed format data like this.
>> > >
>> > > Why would you use unpack when this can be easily split apart with a regex?
>> > > I'd think unpack would be overkill!
>>
>> why is it overkill any more that a regex?
BWM> Are you saying we should be using unpack rather than regular expressions?
try this:
---------------------------------
use strict;
use Benchmark;
my %try =(
'unpack' => sub {
my @dates = qw(2Jul2001 21Jul2001 2Jul2001 21Jul2001 2Jul2001 21Jul2001);
for my $date (@dates)
{
my ($month, $day, $year) =
length $date == 8 ?
unpack 'AA3A4', $date :
unpack 'A2A3A4', $date;
}
},
'regexp' => sub {
my @dates = qw(2Jul2001 21Jul2001 2Jul2001 21Jul2001 2Jul2001 21Jul2001);
foreach (@dates) {
/(\d+)(\D+)(\d+)/;
my ($month, $day, $year) = ( $1,$2,$3);
}
},
);
timethese( 1e5, \%try);
---------------------------------
mak@freebus/usr/home/mak>perl t.pl
Benchmark: timing 100000 iterations of regexp, unpack...
regexp: 15 wallclock secs (14.61 usr + 0.00 sys = 14.61 CPU)
unpack: 12 wallclock secs (11.17 usr + 0.00 sys = 11.17 CPU)
Best wishes,
Maxim mailto:[EMAIL PROTECTED]