in reply to Re: Question about speeding a regexp count
in thread Question about speeding a regexp count

This is probably faster:

my %count; my $length = length $seq; for my $i (0 .. $length - 3) { $count{ substr($seq, $i, 1) }++; $count{ substr($seq, $i, 2) }++; $count{ substr($seq, $i, 3) }++; } $count{ substr($seq, $length - 1, 1) }++; $count{ substr($seq, $length - 2, 1) }++; $count{ substr($seq, $length - 2, 2) }++;

Renamed %seen to the more appropriate %count. Renamed $string to $seq to match the OP.

Replies are listed 'Best First'.
Re^3: Question about speeding a regexp count
by sauoq (Abbot) on Oct 13, 2005 at 20:08 UTC
    This is probably faster:

    Don't forget "less maintainable."

    How many lines do you have to add or change when someone comes around next week and asks you to look at substring lengths up to 10 characters long?

    -sauoq
    "My two cents aren't worth a dime.";
    
      True. Optimization vs Readability/Maintability is always an issue. Only the OP is qualified to answer whether he's willing to sacrifice Readability/Maintability for a 35% increase in speed. (See my benchmarks below.)