Skip to content

Instantly share code, notes, and snippets.

@yappo
Created October 27, 2010 03:20
Show Gist options
  • Save yappo/648375 to your computer and use it in GitHub Desktop.
Save yappo/648375 to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
use common::sense;
use Benchmark qw/cmpthese/;
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