I
it_says_BALLS_on_your forehead
I inherited a script that uses Date::Manip. powerful module, but
painfully slow. Methods from this module are called perhaps over 100
million times for log processing, so even small incremental gains in
speed would accumulate to substantial savings (i hope).
i have replaced Date::Manip with Date::Calc, but i'm losing the padded
0's on the left. what's the fastest way of left-padding the zeroes back
on? just a map? (although i think if i transform a list in place, it's
better to use a for loop per PBP, although Damian Conway says that's
for readability--i'll have to benchmark since this is simple enough i
wouldn't lose much readability, but may gain speed if i use a map---
@new_date = map { /^(\d)$/ and "0$1" or $_ } @new_date;
) i don't want to lose the time i've saved by inefficiently formatting
the output.
currently the input and output is:
========
use strict; use warnings;
use Date::Manip;
my $date = '2006-03-02';
my $time = '05:04:44';
print "original date: $date\n";
print "original time: $time\n";
my $timeDiff = 4;
my $goodTime = DateCalc(ParseDate($date.$time),"-$timeDiff hours");
$date =
substr($goodTime,0,4)."-".substr($goodTime,4,2)."-".substr($goodTime,6,2);
$time = substr($goodTime,8,8);
print "adjusted date: $date\n";
print "adjusted time: $time\n";
__END__
original date: 2006-03-02
original time: 05:04:44
adjusted date: 2006-03-02
adjusted time: 01:04:44
my new script is:
=====
use strict; use warnings;
use Date::Calc qw/ Add_Delta_DHMS /;
my $date2 = '2006-03-02';
my $time2 = '05:04:44';
my $old_date = $date2 . ':' . $time2;
my @pre_date = split /[\-:]/, $old_date;
my @new_date = Add_Delta_DHMS( @pre_date, 0, -4, 0, 0 );
#@new_date = map { /^(\d)$/ and "0$1" or $_ } @new_date;
#print "@new_date\n";
$date2 = join( '-', @new_date[0..2] );
$time2 = join( ':', @new_date[3..$#new_date] );
print "date2: $date2\n";
print "time2: $time2\n";
__END__
date2: 2006-3-2
time2: 1:4:44
# with map
date2: 2006-03-02
time2: 01:04:44
painfully slow. Methods from this module are called perhaps over 100
million times for log processing, so even small incremental gains in
speed would accumulate to substantial savings (i hope).
i have replaced Date::Manip with Date::Calc, but i'm losing the padded
0's on the left. what's the fastest way of left-padding the zeroes back
on? just a map? (although i think if i transform a list in place, it's
better to use a for loop per PBP, although Damian Conway says that's
for readability--i'll have to benchmark since this is simple enough i
wouldn't lose much readability, but may gain speed if i use a map---
@new_date = map { /^(\d)$/ and "0$1" or $_ } @new_date;
) i don't want to lose the time i've saved by inefficiently formatting
the output.
currently the input and output is:
========
use strict; use warnings;
use Date::Manip;
my $date = '2006-03-02';
my $time = '05:04:44';
print "original date: $date\n";
print "original time: $time\n";
my $timeDiff = 4;
my $goodTime = DateCalc(ParseDate($date.$time),"-$timeDiff hours");
$date =
substr($goodTime,0,4)."-".substr($goodTime,4,2)."-".substr($goodTime,6,2);
$time = substr($goodTime,8,8);
print "adjusted date: $date\n";
print "adjusted time: $time\n";
__END__
original date: 2006-03-02
original time: 05:04:44
adjusted date: 2006-03-02
adjusted time: 01:04:44
my new script is:
=====
use strict; use warnings;
use Date::Calc qw/ Add_Delta_DHMS /;
my $date2 = '2006-03-02';
my $time2 = '05:04:44';
my $old_date = $date2 . ':' . $time2;
my @pre_date = split /[\-:]/, $old_date;
my @new_date = Add_Delta_DHMS( @pre_date, 0, -4, 0, 0 );
#@new_date = map { /^(\d)$/ and "0$1" or $_ } @new_date;
#print "@new_date\n";
$date2 = join( '-', @new_date[0..2] );
$time2 = join( ':', @new_date[3..$#new_date] );
print "date2: $date2\n";
print "time2: $time2\n";
__END__
date2: 2006-3-2
time2: 1:4:44
# with map
date2: 2006-03-02
time2: 01:04:44