sub using_index {
our $seq; *seq = \$_[0];
my @groups;
my $pos = -1;
my $start = -1;
for (;;) {
my $new_pos = index($seq, 'M', $pos+1);
if ($new_pos < 0) {
if (defined($start)) {
push(@groups, [ $start, $pos ]);
}
last;
}
if ($start < 0) {
$start = $new_pos;
}
elsif ($new_pos - $pos > 1) {
push(@groups, [ $start, $pos ]);
$start = $new_pos;
}
$pos = $new_pos;
}
return @groups;
}
It would be simpler if there was a function that returned the next character which isn't 'M'.
As you can guess, it's much slower than the regexp approach. The regexp approach is 170% faster than (i.e. 2.7 times the speed of) the index method on the input you provided.
Benchmark code:
Benchmark results:
|