http://qs1969.pair.com?node_id=1176246


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

Depending on how you want to use the information this may help:

use Benchmark qw( cmpthese ); $s = join'', map{ chr( 65+rand(26) ) x rand( 100 ) } 1 .. 1000;; push @first, $1 while $s =~ /((.)\2*)/gs; $s2 = " $s" ^ $s; # XORing the string with a shifted copy of itself, s +o that you have a series of 0s for identical characters push @second, $1 while $s2 =~ /(.\o{0}*)/gs; $\ = $/ x 2; print pack "(A4)*", map length, @first; print pack "(A4)*", map length, @second; cmpthese -1,{ a=>q[ 1 while $s =~ m[((?=(.))\2+)]g; ], b=>q[ 1 while $s =~ m[((.)\2*)]sg; ] , c=>q[$s3 = " $s" ^ $s;; 1 while $s3 =~ /(.\o{0}*)/gs], };;
This first prints the length of the strings found by the two methods (I have removed most of the lines, which don't add any more information):
55 97 65 7 87 60 53 98 2 71 35 68 67 58 12 19 17 22 + 5 28 63 96 30 18 32 6 37 27 47 68 79 97 2 9 60 + 75 87 31 15 82 62 78 33 69 10 35 4 82 61 33 63 82 +96 68 140 88 59 67 87 78 98 14 3 6 52 59 74 86 79 49 44 28 76 + 25 83 99 66 42 67 73 3 46 55 97 65 7 87 60 53 98 2 71 35 68 67 58 12 19 17 22 + 5 28 63 96 30 18 32 6 37 27 47 68 79 97 2 9 60 + 75 87 31 15 82 62 78 33 69 10 35 4 82 61 33 63 82 +96 68 140 88 59 67 87 78 98 14 3 6 52 59 74 86 79 49 44 28 76 + 25 83 99 66 42 67 73 3 46 1
So the second method does give the correct length (plus an extra character because of the shift).

And the benchmark is much faster:

Rate a b c a 445/s -- -4% -80% b 465/s 5% -- -79% c 2228/s 401% 379% --

It does not provide all the information of other methods directly (you still have to get a character in the original string to know what a substring exactly is), but might be useful depending on what you actually need.

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:03 UTC

    That's a nice piece of lateral thinking. I'll have to see if I can find a way of using it without throwing away those significant gains getting the other required info back. Watch this space...


    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.

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

        Sorry for the delay in getting back to you. I've adapted your technique somewhat:

        sub eily{ my $str = shift; my @b = map[ ( 1e99, 0 ) x 2 ], 1 .. 256; for my $y ( 0 .. $HEIGHT-1 ) { my $s = \substr( $$str, $y * $WIDTH, $WIDTH ); my $t = ' ' . $$s ^ $$s; chop $t; while( $t =~ m[[^\0]\0*]g ) { my $c = ord substr $$s, $-[0], 1; #, $-[0], $+[0]; $b[ $c ][ LEFT ] = $-[0] if $-[0] < $b[ $c ][ LEFT + ]; $b[ $c ][ RIGHT ] = $+[0]-1 if $+[0]-1 > $b[ $c ][ RIGH +T ]; $b[ $c ][ TOP ] = $y if $y < $b[ $c ][ TOP + ]; $b[ $c ][ BOTTOM ] = $y if $y > $b[ $c ][ BOTT +OM ]; } } return \@b; }

        and the results are impressive:

        C:\test>\perl22\bin\perl 1176081.pl -WIDTH=10000 -HEIGHT=10000 yr() took 12.478694 buk3() took 8.195635 dave() took 1.876719 eily took 0.888511 C:\test>\perl22\bin\perl 1176081.pl -WIDTH=20000 -HEIGHT=20000 yr() took 18.774189 buk3() took 32.063918 dave() took 5.701784 eily took 2.409520

        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.