Skip to content

Instantly share code, notes, and snippets.

@bokutin
Last active August 29, 2015 14:03
Show Gist options
  • Save bokutin/5f47b901c4d3975442cf to your computer and use it in GitHub Desktop.
Save bokutin/5f47b901c4d3975442cf to your computer and use it in GitHub Desktop.
Perl DateTime.pm vs Python relativedelta
@@ try_datetime.pl
#!/usr/bin/env perl
use Modern::Perl;
use DateTimeX::Web;
# http://www.houseabsolute.com/presentations/a-date-with-perl/#33
{
my $dt = DateTimeX::Web->new->strptime('%Y-%m-%d', '2009-02-01'); # 2009-02-01
$dt->add( days => 28 );
say $dt; # 2009-03-01
$dt->add( days => 28 );
say $dt; # 2009-03-29
}
# http://www.houseabsolute.com/presentations/a-date-with-perl/#34
{
my $dt = DateTimeX::Web->new->strptime('%Y-%m-%d', '2009-01-30'); # 2009-01-30
$dt->add( months => 1 );
say $dt; # 2009-03-02
$dt->add( months => 1 );
say $dt; # 2009-04-02
}
# http://www.houseabsolute.com/presentations/a-date-with-perl/#35
{
my $dt = DateTimeX::Web->new->strptime('%Y-%m-%d', '2009-01-30'); # 2009-01-30
$dt->add( months => 1, end_of_month => 'limit' );
say $dt; # 2009-02-28
$dt->add( months => 1 );
say $dt; # 2009-03-28
}
@@ try_datetime.py
#!/usr/bin/env python
from datetime import date
from dateutil.relativedelta import relativedelta
d1 = date(2009, 2, 1)
d1 = d1 + relativedelta(days=28)
print d1 # 2009-03-01
d1 = d1 + relativedelta(days=28)
print d1 # 2009-03-29
d2 = date(2009, 1, 30)
d2 = d2 + relativedelta(months=1)
print d2 # 2009-02-28
d2 = d2 + relativedelta(months=1)
print d2 # 2009-03-28
@@ sketch/try_datetime_bench1.pl
#!/usr/bin/env perl
use Modern::Perl;
use DateTimeX::Web;
use Timer::Simple ();
my $timer = Timer::Simple->new(start => 0, string => 'rps');
$timer->start;
my $dt = DateTimeX::Web->new->strptime('%Y-%m-%d', '2009-01-30');
for (0..10000) {
$dt->add( months => 1, end_of_month => 'limit' );
}
$timer->stop;
say $timer->string; # 2.406569s (0.416/s)
@@ sketch/try_datetime_bench2.pl
#!/usr/bin/env perl
BEGIN { $ENV{PERL_NO_VALIDATION} = 1 }
use Modern::Perl;
use DateTimeX::Web;
use Timer::Simple ();
my $timer = Timer::Simple->new(start => 0, string => 'rps');
$timer->start;
my $dt = DateTimeX::Web->new->strptime('%Y-%m-%d', '2009-01-30');
my $dur = DateTime::Duration->new( months => 1, end_of_month => 'limit' );
for (0..10000) {
$dt->add_duration($dur);
}
$timer->stop;
say $timer->string; # 1.222190s (0.818/s)
@@ sketch/try_datetime_bench3.pl
#!/usr/bin/env perl
BEGIN { $ENV{PERL_NO_VALIDATION} = 1 }
use Modern::Perl;
use DateTimeX::Web;
use Timer::Simple ();
my $timer = Timer::Simple->new(start => 0, string => 'rps');
$timer->start;
my $dt = DateTimeX::Web->new->strptime('%Y-%m-%d', '2009-01-30');
my $dur = DateTime::Duration->new( months => 1, end_of_month => 'limit' );
$dt->add_duration($dur*10001);
$timer->stop;
say $timer->string; # 0.000963s (1038.422/s)
@@ sketch/try_datetime_bench.py
#!/usr/bin/env python
import time
from datetime import date
from dateutil.relativedelta import relativedelta
t1 = time.time()
d1 = date(2009, 1, 30)
for i in range(0, 10001):
d1 = d1 + relativedelta(months=1)
t2 = time.time()
elapsed = t2 - t1
print elapsed # 0.168323993683
# NYTProfを使ってみた。
# add_duration内で、自身をインクリメントする際に、単純に値を足すのではなく
# DateTime::from_object、DateTime::TimeZone::newなどのオブジェクトを
# 生成し直しているのが遅さの主な原因??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment