Dear Monks
The /p modifier introduced in 5.10 makes ${^PREMATCH}, {$^MATCH}, and ${^POSTMATCH} available for use after matching. It is assumed it is faster than using
$`, $& and $'. But when I benchmark this code:
pl@nereida:~/src/perl/perltesting$ cat ./timeregexpwithp.pl
#!/usr/local/lib/perl/5.10.1/bin//perl5.10.1 -w
use strict;
use Benchmark qw{:all};
cmpthese(
1000000,
{
p => q{'hola juan' =~ /ju/p; my ($a, $b, $c) = (${^PREMATCH}, ${^M
+ATCH}, ${^POSTMATCH} ) },
oldway => q{'hola juan' =~ /ju/; my ($a, $b, $c) = ($`, $&, $') }
}
);
cmpthese(
10000000,
{
pnoassign => q{'hola juan' =~ /ju/p; },
oldwaynoassign => q{'hola juan' =~ /ju/; }
}
);
I've got:
pl@nereida:~/src/perl/perltesting$ ./timeregexpwithp.pl
Rate p oldway
p 892857/s -- -6%
oldway 952381/s 7% --
Rate oldwaynoassign pnoassign
oldwaynoassign 3184713/s -- -1%
pnoassign 3225806/s 1% --
It seems that the old way is faster. Any explanations?
Thanks
-
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.