If we are assuming that isook() is fine functionally, there is a much faster way. Don't use regex! use substr! I ran same benchmarks along with a new one, substr(). Code is below. My machine, (older Win XP running Perl 5.10) is considerably slower than the other machines posting results, but I would expect similar results in the ranking if you run the code below. Having said that, all these solutions run so fast that it is hard to see how this is going to make much difference, but that depends upon the application!
This substr idea is also faster even with some simple format error tests like this snippet... This $1,$2,$3 stuff is "expensive".
substr => sub{
my $t2 = '20090123';
my $year =substr ($t2,0,4);
my $mon = substr ($t2,4,2);
my $day = substr ($t2,6,2);
die if (length($t2)!= 8);
die if ($t2 =~ /\D/);
},
#!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);
},
substr => sub{
my $t2 = '20090123';
my $year =substr ($t2,0,4);
my $mon = substr ($t2,4,2);
my $day = substr ($t2,6,2);
},
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__
output:
Benchmark: timing 1000000 iterations of chkunpk, dirunpk, isook, range
+, repeat, substr...
chkunpk: 4 wallclock secs ( 5.05 usr + 0.00 sys = 5.05 CPU) @
198137.51/s (n=1000000)
dirunpk: 2 wallclock secs ( 3.31 usr + 0.00 sys = 3.31 CPU)
@ 301841.23/s (n=1000000)
isook: 3 wallclock secs ( 3.08 usr + 0.00 sys = 3.08 CPU) @
324886.29/s (n=1000000)
range: 4 wallclock secs ( 3.23 usr + 0.00 sys = 3.23 CPU) @
309214.59/s (n=1000000)
repeat: 4 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @
329924.12/s (n=1000000)
substr: 2 wallclock secs ( 1.22 usr + 0.00 sys = 1.22 CPU) @
820344.54/s (n=1000000)
Rate chkunpk dirunpk range isook repeat substr
chkunpk 198138/s -- -34% -36% -39% -40% -76%
dirunpk 301841/s 52% -- -2% -7% -9% -63%
range 309215/s 56% 2% -- -5% -6% -62%
isook 324886/s 64% 8% 5% -- -2% -60%
repeat 329924/s 67% 9% 7% 2% -- -60%
substr 820345/s 314% 172% 165% 153% 149% --
-
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.