Skip to content

Instantly share code, notes, and snippets.

@acotie
Forked from yappo/bench.pl
Created October 27, 2010 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acotie/648559 to your computer and use it in GitHub Desktop.
Save acotie/648559 to your computer and use it in GitHub Desktop.
#! /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