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, so 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], };;