Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Faster regex to split a string into runs of similar characters?

by dave_the_m (Monsignor)
on Nov 21, 2016 at 12:46 UTC ( [id://1176244]=note: print w/replies, xml ) Need Help??


in reply to Faster regex to split a string into runs of similar characters?

Looking for blocks of the same char speeds it up a bit:
$s = join'', map{ chr( 65+rand(26) ) x rand( 100 ) } 1 .. 1000;; cmpthese( -1,{ a=>q[ 1 while $s =~ m[((?=(.))\2+)]g; ], b=>q[ 1 while $s =~ m[((?=(.))\2+)]sg; ] , c=>q[ 1 while $s =~ m/((.)\2*)/sg; ], d=>q[ 1 while $s =~ m{ ( (?=(.)) (?: (\2\2\2\2\2\2\2\2) \3* | (\2\2\2\2) \4* | (\2\2) \5* | \2+ )+ ) }sgx; ], }); Rate b a c d b 193/s -- -0% -3% -35% a 194/s 0% -- -2% -35% c 198/s 3% 2% -- -33% d 296/s 54% 53% 50% --

Dave.

Replies are listed 'Best First'.
Re^2: Faster regex to split a string into runs of similar characters?
by BrowserUk (Patriarch) on Nov 21, 2016 at 14:01 UTC

    Never would have thought of that! It yeilds another nice chunk of savings in the real application:

    C:\test>\perl22\bin\perl 1176081.pl -WIDTH=1000 -HEIGHT=1000 yr() took 2.551594 buk() took 1.068262 buk2() took 0.681494 buk3() took 0.167000 dave() took 0.127978

    Thanks.

    On a related note: Any ideas why this:

    while( substr( $$str, $y * $WIDTH, $WIDTH ) =~ m[((.)\2*)]mg ) { ...

    Works (runs to completion, produces the desired results) on 5.10.1, but silently loops forever in 5.22.0?

    This works in 5.22.0:

    my $ref = \substr( $$str, $y * $WIDTH, $WIDTH ); while( $$ref =~ m[((.)\2*)]mg ) {

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.
      On a related note: Any ideas why this:
      while( substr( $$str, $y * $WIDTH, $WIDTH ) =~ m[((.)\2*)]mg ) { ...
      Works (runs to completion, produces the desired results) on 5.10.1, but silently loops forever in 5.22.0?
      Looks like a bug fix. expr =~ /.../g attaches position magic to the LHS after each match, to record the current pos() for that expression. If expr is a var or similar this works well; if expr returns something new each time, the old pos magic gets lost.

      This similarly loops forever:

      sub f { "abc" } 1 while f() =~ /./g;

      Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176244]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found