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 | |
by dave_the_m (Monsignor) on Nov 21, 2016 at 14:16 UTC |
In Section
Seekers of Perl Wisdom