in reply to Re: More efficient way to truncate long strings of the same character
in thread More efficient way to truncate long strings of the same character

s/((.)\1{2})\1+/$2/g

I think you might have mixed up your '1's and '2's.

use strict; use warnings; my $str = q{aabbbbcddddddee}; print qq{Starting string\n $str\n}; # Your regex. $str =~ s/((.)\1{2})\1+/$2/g; print qq{Original regex\n $str\n}; # I think this is correct. $str = q{aabbbbcddddddee}; $str =~ s/((.)\2{2})\2+/$1/g; print qq{Corrected regex\n $str\n};

Produces

Starting string aabbbbcddddddee Original regex aabbbbcddddddee Corrected regex aabbbcdddee

This might affect the benchmark a little.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: More efficient way to truncate long strings of the same character
by mr_mischief (Monsignor) on Oct 30, 2008 at 23:37 UTC
    It does change the benchmark for me somewhat. Under 5.10.0 on my 1GHz system, your fixed version of GrandFather's substitution actually performed better for the longer string 4 out of 5 runs of the benchmark.

    Interestingly enough, under 5.10.0 on this system, the original version posted is always one of the three fastest on the shorter string (and usually the fastest other than GrandFather's version). It's usually about 25% faster than the next. Under 5.8.8 on the same system, it's in a dead heat for the top for the longer string as well. 5.10.0 moves it back quite a bit in the rankings on the longer string, but it's still quite respectable in comparison.

    This reeks of premature optimization, but perhaps it isn't.