We don't bite newbies here... much PerlMonks

### Re: Simple date and time manipulation

by andreas1234567 (Vicar)
 on Jan 19, 2011 at 09:57 UTC ( #883075=note: print w/replies, xml ) Need Help??

in reply to Simple date and time manipulation

It depends on your definition of "slow". I can create 10000 timestamps in ~3 seconds (Increased from 1000 to 10000 for more reliable count) with DateTime, which is also considered "slow" by some.
```use strict;
use warnings;
use DateTime;
use Benchmark qw(timethis);

my \$dt = DateTime->new(
year   => 2011,
month  => 1,
day    => 1,
hour   => 0,
minute => 0,
second => 0,
);

timethis(
10000,
sub {
print \$dt->ymd() . ' ' . \$dt->hms;
\$dt->add(hours => 1, minutes => 2, seconds => 3);
}
);

timethis(
10000,
sub {
\$dt->add(hours => 1, minutes => 2, seconds => 3);
}
);

__END__
# Calc + print
timethis 10000:  3 wallclock secs ( 3.09 usr +  0.06 sys =  3.15 CPU)
+@ 3174.60/s (n=10000)
# Just calc:
timethis 10000:  3 wallclock secs ( 2.95 usr +  0.00 sys =  2.95 CPU)
+@ 3389.83/s (n=10000)
If you are really in a hurry, the general consensus is to use Date::Calc. However, with it you would have to do more of the sprintf() formatting yourself.
```use strict;
use warnings;
use DateTime;
use Benchmark qw(cmpthese);

my \$dt = DateTime->new(
year   => 2011,
month  => 1,
day    => 1,
hour   => 0,
minute => 0,
second => 0,
);

my (\$year, \$month, \$day, \$hour, \$min, \$sec) = (2011, 1, 1, 0, 0, 0);

cmpthese(
-1,
{
'DateTime' => sub {
print \$dt->ymd() . ' ' . \$dt->hms;
\$dt->add(hours => 1, minutes => 2, seconds => 3);
},
'Date::Calc' => sub {
print \$year . '-' . \$month . '-' . \$day . ' ' . \$hour . '-' . \$m
+in . '-'
. \$sec;
(\$year, \$month, \$day, \$hour, \$min, \$sec) =
Add_Delta_DHMS(\$year, \$month, \$day, \$hour, \$min, \$sec, 0, 1, 2
+, 3);
},
}
);

__END__

\$ perl -l 883070.pl | tail -5
2011-07-18 10:20:15
2011-07-18 11:22:18
Rate   DateTime Date::Calc
DateTime     3258/s         --       -99%
Date::Calc 481882/s     14690%         --
\$
Updated Wed Jan 19 11:12:58 CET 2011 with Date::Calc example.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Create A New User
Node Status?
node history
Node Type: note [id://883075]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-04-26 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (94 votes). Check out past polls.

Notices?