Last active
September 1, 2015 10:11
-
-
Save MacoTasu/44ec9df3f535d68d5d19 to your computer and use it in GitHub Desktop.
Benchmark DateTime vs DateTime::epoch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use utf8; | |
use warnings; | |
use DateTime; | |
use Benchmark qw(:all) ; | |
my $now_1 = DateTime->now; | |
my $now_2 = $now_1->clone; | |
my @times = ($now_1, $now_2); | |
my $today = DateTime->today; | |
cmpthese(0, { | |
'datetime' => sub { | |
[grep { $_ >= $today } @times]; | |
}, | |
'epoch' => sub { | |
[grep { $_->epoch >= $today->epoch } @times]; | |
}, | |
}); | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
epoch
epochの実装をみると
のようなになっていて、utc_rd_*などに日にちと秒数が1970年1月1日までの経過日数を引いてそこに1日に秒数をかけて、エポック秒に変換している。
単純に数字の変換の処理と比較しか走っていないので速い。
datetime
Datetimeオブジェクトの比較は、闇から始まっていてoperatorをoverloadしている。
ゆるふわ脳なので、なんかPerlがよしなにしてくれてるんだろと思ったけどそんなことはなかった。
比較しているところは
こんな感じの実装になっていたところまで確認したけど、不思議なことに途中からの記憶が無い。