in reply to Re: how to find this substring?
in thread how to find this substring?

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.

Replies are listed 'Best First'.
Re^3: how to find this substring?
by GrandFather (Saint) on Jun 09, 2012 at 01:53 UTC

    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% --
        Write it correctly the first time

        Yes, exactly. Then dirty it up to make it go faster if you must. Your sample code is an excellent case in point. If you were processing vast numbers of strings in a highly time critical application and were doing no other processing at all then there may be some justification for using index instead of a regex match and performing the extra checking required to detect a failed match. Otherwise it's a lot clearer to go with the simple regex.

        It doesn't help your argument that your unchecked substr sample code gives what would probably be considered the wrong answer when there is no match: the last character of the string. Correct handling of the no match case dirties up the index code and doubles its execution time, but the humble regex is essentially unaffected. Consider:

        use strict; use warnings; use Benchmark 'cmpthese'; my $str = 'FOO' x 14 . 'ZOOCOO' . ('FOO' x 7); for my $subName (qw(doSubZ doSubB doSubX doRegZ doRegB)) { my $result = main->can($subName)->(); $result //= '--undef--'; print "$subName: $result\n"; } cmpthese( -1, { 'substrindexZ' => \&doSubZ, 'substrindexB' => \&doSubB, 'regexpZ' => \&doRegZ, 'regexpB' => \&doRegB, } ); sub doSubZ { return undef if -1 == (my $idx = index ($str, 'ZOO')); return substr ($str, $idx, 6); } sub doSubX { return substr ($str, index ($str, 'BOO'), 6); } sub doSubB { return undef if -1 == (my $idx = index ($str, 'BOO')); return substr ($str, $idx, 6); } sub doRegZ { $str =~ /(ZOO.{3})/; return $1; } sub doRegB { $str =~ /(BOO.{3})/; return $1; }

        Prints:

        doSubZ: ZOOCOO doSubB: --undef-- doSubX: O doRegZ: ZOOCOO doRegB: --undef-- Rate regexpZ substrindexZ substrindexB reg +expB regexpZ 1215237/s -- -45% -70% +-79% substrindexZ 2198729/s 81% -- -45% +-62% substrindexB 4003188/s 229% 82% -- +-30% regexpB 5733702/s 372% 161% 43% + --
        True laziness is hard work