Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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]

In reply to Re: Simple date and time manipulation by andreas1234567
in thread Simple date and time manipulation by tsk1979

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (12)
    As of 2014-07-31 17:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (249 votes), past polls