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 Date::Calc qw (Add_Delta_DHMS);
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]
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.