Skip to content

Instantly share code, notes, and snippets.

@oklahomer
Created March 20, 2014 16:02
Show Gist options
  • Save oklahomer/9667189 to your computer and use it in GitHub Desktop.
Save oklahomer/9667189 to your computer and use it in GitHub Desktop.
#!/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