-
-
Save acotie/648559 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 common::sense; | |
use Benchmark qw/cmpthese/; | |
use Perl6::Say; | |
say $^V; | |
my $datetime = '20101028120000'; | |
cmpthese(1000000, { | |
unpack => sub { | |
my($year, $month, $day, $hour, $minute, $second) = unpack 'a4a2a2a2a2a2', $datetime; | |
}, | |
regexp => sub { | |
my($year, $month, $day, $hour, $minute, $second) = $datetime =~ /^(....)(..)(..)(..)(..)(..)$/; | |
}, | |
substr => sub { | |
my $year = substr $datetime, 0, 4; | |
my $month = substr $datetime, 4, 2; | |
my $day = substr $datetime, 6, 2; | |
my $hour = substr $datetime, 8, 2; | |
my $minute = substr $datetime, 10, 2; | |
my $second = substr $datetime, 12, 2; | |
}, | |
}); | |
__END__ | |
v5.10.0 | |
Rate regexp unpack substr | |
regexp 230947/s -- -53% -79% | |
unpack 492611/s 113% -- -55% | |
substr 1098901/s 376% 123% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment