Skip to content

Instantly share code, notes, and snippets.

@bokutin
Last active August 29, 2015 14:06
Show Gist options
  • Save bokutin/326e09a1cc2272f584dc to your computer and use it in GitHub Desktop.
Save bokutin/326e09a1cc2272f584dc to your computer and use it in GitHub Desktop.
Time::Momentさんかりっかり
#!/usr/bin/env perl
use Modern::Perl;
use Benchmark qw(:all) ;
use Data::Dumper;
use DateTimeX::Web;
use DateTime::Span;
use Date::Manip::Date;
use Date::Manip::Recur;
use Date::Calc qw(:all);
use Time::Moment;
use Time::Piece ();
use Time::Seconds;
my $START = '2014-05-15';
my $END = '2014-06-14';
my $START2 = '20140515';
my $END2 = '20140614';
sub dates_with_datetime {
my $set = DateTime::Set->from_recurrence(
recurrence => sub {
return $_[0] if $_[0]->is_infinite;
return $_[0]->add( days => 1 );
},
);
my $start = DateTimeX::Web->new->strptime('%Y-%m-%d', $START);
my $end = DateTimeX::Web->new->strptime('%Y-%m-%d', $END);
my $span = DateTime::Span->new( start => $start, end => $end );
my @days = $set->as_list( span => $span );
0+@days;
}
sub dates_with_datetime2 {
my $set = DateTime::Set->from_recurrence(
recurrence => sub {
return $_[0] if $_[0]->is_infinite;
return $_[0]->add( days => 1 );
},
);
state $start = DateTimeX::Web->new->strptime('%Y-%m-%d', $START);
state $end = DateTimeX::Web->new->strptime('%Y-%m-%d', $END);
my $span = DateTime::Span->new( start => $start, end => $end );
my @days = $set->as_list( span => $span );
0+@days;
}
sub dates_with_datetime3 {
state $set = DateTime::Set->from_recurrence(
recurrence => sub {
return $_[0] if $_[0]->is_infinite;
return $_[0]->add( days => 1 );
},
);
state $start = DateTimeX::Web->new->strptime('%Y-%m-%d', $START);
state $end = DateTimeX::Web->new->strptime('%Y-%m-%d', $END);
state $span = DateTime::Span->new( start => $start, end => $end );
my @days = $set->as_list( span => $span );
0+@days;
}
sub dates_with_datemanip {
my $recur = Date::Manip::Recur->new;
$recur->parse('every day', undef, undef, $START, $END);
die $recur->err if $recur->err;
my @days = $recur->dates;
0+@days;
}
sub dates_with_datemanip2 {
state $start = Date::Manip::Date->new($START);
state $end = Date::Manip::Date->new($END);
state $recur = do {
my $recur = Date::Manip::Recur->new;
$recur->parse('every day', undef, undef, $start, $end) && die $recur->err;
$recur;
};
my @days = $recur->dates;
0+@days;
}
sub dates_with_datemanip3 {
state $start = Date::Manip::Date->new($START);
state $end = Date::Manip::Date->new;
#$end->parse_date($END);
#$end->parse_format('%Y-%m-%d', $END);
$end->set( date => [2014,6,14,0,0,0] ) && die $end->err;
state $recur = do {
my $recur = Date::Manip::Recur->new;
$recur->parse('every day', undef, undef, $start, $end) && die $recur->err;
$recur;
};
my @days = $recur->dates;
0+@days;
}
sub dates_with_datecalc {
my ($year1,$month1,$day1) = split /-/, $START;
my ($year2,$month2,$day2) = split /-/, $END;
my $Dd = Delta_Days($year1,$month1,$day1,$year2,$month2,$day2);
my @days = map {
my ($year,$month,$day) = Add_Delta_Days($year1,$month1,$day1, $_);
sprintf "%04d-%02d-%02d", $year,$month,$day;
} (0..$Dd);
#die Dumper \@days;
0+@days;
}
sub dates_with_timemoment {
my $start = Time::Moment->from_string("${START2}T000000Z");
my $end = Time::Moment->from_string("${END2}T000000Z");
my @days;
while (1) {
push @days, $start;
last unless $start < $end;
$start = $start->plus_days(1);
}
#die Dumper [ map { $_."" } @days ];
0+@days;
}
sub dates_with_timemoment2 {
my $start = Time::Moment->from_string("${START2}T000000Z");
my $end = Time::Moment->from_string("${END2}T000000Z");
my $dd = ($end->utc_rd_values)[0] - ($start->utc_rd_values)[0];
my @days = map { $start->plus_days($_) } (0..$dd);
0+@days;
}
sub dates_with_timepiese {
my $start = Time::Piece->strptime($START, "%Y-%m-%d");
my $end = Time::Piece->strptime($END, "%Y-%m-%d");
my @days;
while (1) {
push @days, $start;
last unless $start < $end;
$start += ONE_DAY;
}
0+@days;
}
say "$_: " . main->can($_)->() for @tests;
#my @tests = sort grep /^dates_/, keys %main::;
#my $count = 100;
my @tests = sort grep /^dates_with_timemoment/, keys %main::;
my $count = 100000;
cmpthese($count, {
map { $_ => main->can($_)||die } @tests
});
__END__
% sketch/try_span_dates.pl
dates_with_datecalc: 31
dates_with_datemanip: 31
dates_with_datemanip2: 31
dates_with_datemanip3: 31
dates_with_datetime: 31
dates_with_datetime2: 31
dates_with_datetime3: 31
dates_with_timemoment: 31
dates_with_timepiese: 31
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
Rate dates_with_datetime dates_with_datetime2 dates_with_datetime3 dates_with_datemanip dates_with_datecalc dates_with_timepiese dates_with_datemanip3 dates_with_datemanip2 dates_with_timemoment
dates_with_datetime 28.8/s -- -6% -16% -17% -99% -99% -100% -100% -100%
dates_with_datetime2 30.8/s 7% -- -11% -11% -99% -99% -100% -100% -100%
dates_with_datetime3 34.5/s 20% 12% -- -1% -98% -99% -99% -100% -100%
dates_with_datemanip 34.7/s 20% 13% 1% -- -98% -99% -99% -100% -100%
dates_with_datecalc 2133/s 7300% 6833% 6083% 6050% -- -17% -67% -83% -100%
dates_with_timepiese 2560/s 8780% 8220% 7320% 7280% 20% -- -60% -80% -100%
dates_with_datemanip3 6400/s 22100% 20700% 18450% 18350% 200% 150% -- -50% -100%
dates_with_datemanip2 12800/s 44300% 41500% 37000% 36800% 500% 400% 100% -- -100%
dates_with_timemoment 100000000000000000/s 346874999999999936% 325000000000000000% 289843749999999936% 288281249999999936% 4687500000000000% 3906250000000000% 1562500000000000% 781250000000000%
% sketch/try_span_dates.pl
dates_with_timemoment: 31
dates_with_timemoment2: 31
Rate dates_with_timemoment dates_with_timemoment2
dates_with_timemoment 28635/s -- -23%
dates_with_timemoment2 37318/s 30% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment