in reply to how to find this substring?

I wonder if index & rindex might do a better job, particularly if the strings to search are large.

so something along these lines

my $start = index ($dna,'ATG'); my $end1 = rindex($dna,'TAA'); my $end2 = rindex($dna,'TAG'); ...

Obviously, you'd need a little logic to handle the different stop strings, but I've left that as an exercise for the reader ;)

Replies are listed 'Best First'.
Re^2: how to find this substring?
by bulk88 (Priest) on Jun 08, 2012 at 17:04 UTC
    Index is in my testing 10x faster than the regexp of the same search. So 1 or 2 indexes and 2-3 control branches and a substr is faster than the regexp that does the same. Always use index where possible.

      No. Only use index where the code is clearer or there is a clear and necessary speed advantage.

      In other words: in general write for clarity (and thus correctness and maintainability) first. If speed is an issue focus on the bang for buck areas of the code using profiling and address bottle necks as they become apparent and significant.

      True laziness is hard work
        Write it correctly the first time, unless your being paid by the hour. Comments are for clarity. Code is not meant to be self documenting.
        use strict; use warnings; use Benchmark ':hireswallclock', 'cmpthese'; my $str = "FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOZOOCOOFOOFOOFOOFO +OFOOFOOFOO"; my $pos = 0; my $res = ''; cmpthese(2000000, {'substrindex' => sub { $res = substr($str, index($str, 'ZOO'), 6); #print $res."\n"; }, 'regexp' => sub{ $str =~ /(ZOO.{3})/; $res = $1; #print $res."\n"; } } );
        Rate regexp substrindex regexp 627353/s -- -81% substrindex 3367003/s 437% --