"be consistent" PerlMonks

### Re: Solution found!

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

in thread Simple date and time manipulation

It works fast and is tiny.
I beg to differ. It is both slow and verbose. DateTime is 10 times faster, and Date::Calc is 700 times faster!
```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 {
\$dt->add(hours => 1, minutes => 2, seconds => 3);
},
'Date::Calc' => sub {
(\$year, \$month, \$day, \$hour, \$min, \$sec) =
Add_Delta_DHMS(\$year, \$month, \$day, \$hour, \$min, \$sec, 0, 1, 2
+, 3);
},
'tsk1979' => sub {
my \$lat     = 23.438;
my \$slon    = 68.000;
my \$elon    = 90.000;
my \$clon    = \$slon;
my \$incrlon = 0.01;
my \$time    = time;
my (\$sec, \$min, \$hour, \$mday, \$mon, \$year, \$wday, \$yday, \$isdst)
+ =
localtime(\$time);
my \$incrtime = 1;
my \$printmon;
my \$printyear;

while (\$clon < \$elon) {
\$printmon  = \$mon + 1;
\$printyear = \$year + 1900;
\$clon = \$clon + \$incrlon;
\$time = \$time + \$incrtime;
(\$sec, \$min, \$hour, \$mday, \$mon, \$year, \$wday, \$yday, \$isdst)
+=
localtime(\$time);
}
},
}
);

__END__

\$ perl -l 883070.pl | tail -5
Rate    tsk1979   DateTime Date::Calc
tsk1979       339/s         --       -90%      -100%
DateTime     3319/s       878%         --      -100%
Date::Calc 893673/s    263192%     26830%         --
\$
It is a good habit to Benchmark your code before making bold claims about its performance.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Replies are listed 'Best First'.
Re^2: Solution found!
by ruzam (Curate) on Jan 19, 2011 at 17:35 UTC

What? You're going to compare 16 lines of unrelated code (which contains a nasty loop) to a single line function and call it a benchmark victory? The OP was looking for a quick date time increment. His solution contained this and plenty of other code not directly related to the date time increment. If you're going to benchmark you won't get very far comparing apples to oranges.

By my benchmarking, the OP's "equivalent datetime increment" code is 144 times faster than DateTime and nearly as fast as Date::Calc.

It's a good habit to check that your Benchmark code is equivalent before making bold claims about your Benchmark results.

Create A New User
Node Status?
node history
Node Type: note [id://883087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2021-01-26 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (271 votes). Check out past polls.

Notices?