Well if instead of the length of the strings you save the pos, getting the information from the first string is straightforward (and O(1) if you only need one character). It removes the need to copy the substrings altogether, since you can access them directly in the input string. It does look like you still get a significant gain when copying all the substrings into the array:
cmpthese -1,{
a_copy=>q[
@array = ();
push @array, "$1" while $s =~ m[((.)\2*)]sg;
] ,
a_pos=>q[
@array = ();
push @array, pos() while $s =~ m[(.)\1*]sg;
] ,
b_cow=>q[ # There might be a COW mechanism because of the call to
+substr
@array = ();
$s3 = " $s" ^ $s;; push @array, substr($s, pos(), length
+ $1) while $s3 =~ /(.\o{0}*)/gs
],
b_copy=>q[ # Force copy, to avoid delayed penalty of COW
@array = ();
$s3 = " $s" ^ $s;; push @array, "".substr($s, pos(), l
+ength $1) while $s3 =~ /(.\o{0}*)/gs
],
b_pos=>q[
@array = ();
$s3 = " $s" ^ $s;; push @array, pos() while $s3 =~ /.\o{
+0}*/gs
],
};;
__DATA__
Rate a_copy a_pos b_copy b_cow b_pos
a_copy 383/s -- -20% -49% -61% -80%
a_pos 478/s 25% -- -36% -51% -75%
b_copy 747/s 95% 56% -- -23% -60%
b_cow 971/s 154% 103% 30% -- -49%
b_pos 1888/s 393% 295% 153% 94% --
-
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.