Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > what is a good/fast way to format date string?

Reply
Thread Tools

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

 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      03-06-2006
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,;

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

 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      03-06-2006
it_says_BALLS_on_your forehead wrote:

> 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 )
);
 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      03-06-2006

J. Gleixner wrote:
> it_says_BALLS_on_your forehead wrote:
>
> > 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 )
> );


excellent, just what i was looking for! thanks!

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      03-06-2006

Christian Winter wrote:
> 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments