I'd expect that the unpack is the faster method to split the fields, but after inserting the following code in the above benchmark, in the average "dirunpk" (direct unpack) took the same amount of time than the "repeat" test.
#!perl
use v5.10;
use strict;
use warnings;
use Benchmark qw(:all);
my $results = timethese( 1e6,
{
repeat => sub{
my $t1 = '20090123';
$t1 =~ /(\d\d\d\d)(\d\d)(\d\d)/;
my ($y1,$m1,$d1) = ($1,$2,$3);
},
range => sub{
my $t2 = '20090123';
$t2 =~ /(\d{4})(\d{2})(\d{2})/;
my ($y2,$m2,$d2) = ($1,$2,$3);
},
chkunpk => sub{
my $t3 = '20090123';
$t3 =~ m/([0-9]{8})/;
my ($y3,$m3,$d3) = unpack "A4 A2 A2", $1;
},
dirunpk => sub{
my $t3 = '20090123';
my ($y4,$m4,$d4) = unpack "A4 A2 A2", $t3;
},
isook => sub{
my $t5 = '20090123';
$t5 =~ /(....)(..)(..)/;
my ($y5,$m5,$d5) = ($1,$2,$3);
},
} );
cmpthese( $results ) ;
__END__
1st run:
Benchmark: timing 1000000 iterations of chkunpk, dirunpk, isook, range
+, repeat...
chkunpk: 4 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 32
+1646.83/s (n=1000000)
dirunpk: 2 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 48
+4966.05/s (n=1000000)
isook: 1 wallclock secs ( 1.95 usr + 0.00 sys = 1.95 CPU) @ 51
+2032.77/s (n=1000000)
range: 3 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 46
+3821.89/s (n=1000000)
repeat: 2 wallclock secs ( 1.97 usr + 0.00 sys = 1.97 CPU) @ 50
+8130.08/s (n=1000000)
Rate chkunpk range dirunpk repeat isook
chkunpk 321647/s -- -31% -34% -37% -37%
range 463822/s 44% -- -4% -9% -9%
dirunpk 484966/s 51% 5% -- -5% -5%
repeat 508130/s 58% 10% 5% -- -1%
isook 512033/s 59% 10% 6% 1% --
2nd run:
Benchmark: timing 1000000 iterations of chkunpk, dirunpk, isook, range
+, repeat...
chkunpk: 2 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 32
+1646.83/s (n=1000000)
dirunpk: 3 wallclock secs ( 2.05 usr + 0.00 sys = 2.05 CPU) @ 48
+8519.79/s (n=1000000)
isook: 2 wallclock secs ( 1.98 usr + 0.00 sys = 1.98 CPU) @ 50
+4032.26/s (n=1000000)
range: 1 wallclock secs ( 2.11 usr + 0.00 sys = 2.11 CPU) @ 47
+4158.37/s (n=1000000)
repeat: 3 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 48
+4966.05/s (n=1000000)
Rate chkunpk range repeat dirunpk isook
chkunpk 321647/s -- -32% -34% -34% -36%
range 474158/s 47% -- -2% -3% -6%
repeat 484966/s 51% 2% -- -1% -4%
dirunpk 488520/s 52% 3% 1% -- -3%
isook 504032/s 57% 6% 4% 3% --
The faster way seems to be using the capture made of dots as in "isook", as the OP said that the string IS a date in ISO format.
UPDATE: Name of tests changed (for readability) and comparison table added. Results are for two consecutive runs.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.