Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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 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]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://883075]
help
Chatterbox?
[Eily]: you could tie a variable into not having the same value each time, if you like to make people who try to debug your code facepalm
[Corion]: perl -wle 'package o; use overload q("") => sub {warn "str"; ""}, bool => sub{warn "bool"; 1}; package main; my $o={}; bless $o => o; print "Yay" if ($o && !length($o))'
[Corion]: But people writing such code should document the objects they construct and why it makes sense for an object to be invisible as string while being true in a boolean context
[hippo]: That's equal parts clever and horrendous.
[Eily]: the overload version wouldn't return true with "$x" && !length $x though, I guess
[hippo]: The more I look at this code, the more $x is a plain old scalar and the more this condition will never be true. I'm calling it a bug at this point.
[hippo]: Thanks for your input which has soothed my sanity (a little)
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-07-27 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (413 votes). Check out past polls.