in reply to Re^2: Faster regex to split a string into runs of similar characters?
in thread Faster regex to split a string into runs of similar characters?
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% --
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Faster regex to split a string into runs of similar characters?
by BrowserUk (Patriarch) on Nov 21, 2016 at 23:09 UTC | |
by vr (Curate) on Nov 22, 2016 at 14:14 UTC | |
by BrowserUk (Patriarch) on Nov 22, 2016 at 14:52 UTC | |
by Eily (Monsignor) on Nov 22, 2016 at 16:15 UTC | |
by BrowserUk (Patriarch) on Nov 22, 2016 at 16:34 UTC |
In Section
Seekers of Perl Wisdom