Created
March 20, 2014 16:02
-
-
Save oklahomer/9667189 to your computer and use it in GitHub Desktop.
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/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Modern::Perl; | |
use Benchmark qw(cmpthese); | |
use DateTime; | |
use DateTime::Format::Strptime; | |
use Time::Piece qw(localtime); | |
use POSIX qw(floor strftime); | |
my $birthday = '1985-09-08'; | |
say "Perl: $]"; | |
say "DateTime: $DateTime::VERSION"; | |
say "Time::Piece: $Time::Piece::VERSION"; | |
say "POSIX: $POSIX::VERSION"; | |
#Perl: 5.018002 | |
#DateTime: 1.08 | |
#Time::Piece: 1.20_01 | |
#POSIX: 1.32 | |
# Rate DateTime Time::Piece POSIX | |
#DateTime 614/s -- -98% -99% | |
#Time::Piece 24752/s 3930% -- -47% | |
#POSIX 46729/s 7507% 89% -- | |
cmpthese ( | |
50_000, | |
+{ | |
'DateTime' => sub { | |
( | |
DateTime->now() | |
- DateTime::Format::Strptime->new( | |
pattern => '%Y-%m-%d', | |
time_zone => 'local' | |
)->parse_datetime($birthday) | |
)->years; | |
}, | |
'Time::Piece' => sub { | |
int( | |
( | |
localtime() - Time::Piece->strptime($birthday, "%Y-%m-%d") | |
)->years | |
); | |
}, | |
'POSIX' => sub { | |
floor( | |
( | |
strftime(q{%Y%m%d}, localtime) | |
- join( '', split('-', $birthday) ) | |
) / 10_000 | |
); | |
}, | |
} | |
); | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment