in reply to using s/// as map?
in thread how can I speed up this perl??

I use it a lot in perlgolf, but not in real code since it tends to destroy the string (notice that your code does, though you tried to avoid that I think). In real code using a while on a regex in scalar context is slightly faster and not so dangerous. When programming an inner loop and trying to be blazingly fast, you also should avoid things like $1.$2 since constructing a new value is a relatively expensive operation in perl. So the regex variant I'd use is:
$count{$1}++ while $genome =~ /(?=(..))./g
But this is still twice as slow as the substr() variant.

Replies are listed 'Best First'.
Re: Re: using s/// as map?
by Roy Johnson (Monsignor) on Nov 24, 2003 at 19:29 UTC
    This should be a little better.
    ++$count{$1} while $genome =~ /\G(..)/g;
    The PerlMonks advocate for tr///
      You're only doing half the work now :-)

      You can make it work like this though:

      ++$count{$1},pos($genome)-- while $genome =~ /\G(..)/g
      It's interesting that this is slightly faster than dropping the \G.

      This however turns out to be a lot slower (seems to do a complete linear search for the proper string start every time):

      $genome =~ /./g; $count{$1}++ while $genome =~ /(.\G.)/g

      update This however means that the lookahead version can be speeded up by about 5% too:

      $count{$1}++ while $genome =~ /\G(?=(..))./g
        You're only doing half the work now :-)
        But if I understand gene work (not a certainty at all), I'm doing the half they actually want to do. :-P A molecule isn't paired with both of its neighbors, they are grouped two-by-two.

        At least now I understand why they were doing the lookahead.

        The PerlMonks advocate for tr///