Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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% --

In reply to Re^3: Faster regex to split a string into runs of similar characters? by Eily
in thread Faster regex to split a string into runs of similar characters? by BrowserUk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2023-06-02 19:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found