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
|
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. | [reply] [d/l] |
|
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.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
sub f { "abc" }
1 while f() =~ /./g;
Dave. | [reply] [d/l] [select] |
Re: Faster regex to split a string into runs of similar characters?
by Eily (Monsignor) on Nov 21, 2016 at 13:27 UTC
|
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.
| [reply] [d/l] [select] |
|
| [reply] |
|
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% --
| [reply] [d/l] |
|
|
|
Re: Faster regex to split a string into runs of similar characters?
by Eily (Monsignor) on Nov 21, 2016 at 10:26 UTC
|
If you want to match all 256 byte values, you'll need the /s modifier. I thought this would just be more correct, but it also happen to be slightly faster (4% on my computer), certainly because it's faster to match anything rather than check that the character is different from "\n".
Beside that, why are you using look-ahead assertions? Isn't /((.)\2+)/g stricly identical to your regex? I don't see a performance difference with that though, so maybe perl optimizes away the look-ahead.
| [reply] [d/l] |
|
| [reply] |
|
| [reply] [d/l] |
|
Re: Faster regex to split a string into runs of similar characters?
by Anonymous Monk on Nov 21, 2016 at 12:04 UTC
|
So some kind of run-length encoding? Maybe use existing fast C code? | [reply] |
|
| [reply] |