what is a good/fast way to format date string?

  • Thread starter it_says_BALLS_on_your forehead
  • Start date
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
 
J

J. Gleixner

it_says_BALLS_on_your forehead said:
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?
#@new_date = map { /^(\d)$/ and "0$1" or $_ } @new_date;

Using sprintf is the way to go. In addition to being much
more readable, it's much faster. Using one sprintf call, seemed
to provide very good results.

($date2, $time2 ) = split (' ',
sprintf("%d-%02d-%02d %02d:%02d:%02d",
@new_date )
);
 
I

it_says_BALLS_on_your forehead

J. Gleixner said:
Using sprintf is the way to go. In addition to being much
more readable, it's much faster. Using one sprintf call, seemed
to provide very good results.

($date2, $time2 ) = split (' ',
sprintf("%d-%02d-%02d %02d:%02d:%02d",
@new_date )
);

excellent, just what i was looking for! thanks!
 
I

it_says_BALLS_on_your forehead

Christian said:
it_says_BALLS_on_your forehead schrieb:
[...]
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;

Normally a sprintf should be cheaper than applying a regex.
@new_date = map { sprintf "%02i", $_ } @new_date;

But you might get even more speed if you omit the re-assignment
of the list at all:

$_ = sprintf( "%02i", $_ ) for( @newdate );

thank you Chris!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top