Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

A seemingly very simple question which I have searched pretty hard already, honest. I'm writing proteomics code in Perl and spend a lot of time scanning down long protein strings, like


These can be 50 to 5000 chars long. I have heaps of code like

for $i (0 .. $max) { $c = substr($protein, $i, 1); # Process $c etc etc }

Profiling the code with Devel::FastProf I find that lots of time goes in the "for" loop and the "substr" call, not unexpectedly. Now, I'm pretty efficient with general coding and string handling, and have optimised piles of regexp matches after trying variants under FastProf, etc etc but to my surprise I can't find an efficient special "walk down a string" technique. Other languages have "iterators" which understand special cases like a plain walk along a string and give you an optimised interface, ie they remember where you are in the string and can give you the next char with basically one op.

I can split the string into an array with @array = split( //, $protein) and use array indexing, but this seems to only gain 20% in the best test case, and about nothing in my real code, ie the cost of splitting nullifies the gain. The protein strings are re-used about 20 times, so the split cost is amortised a little, but the gain is small. To give you a tiny flavour of the data sizes: 40,000 proteins, average lnegth 400, each protein scanned 20 times.

Ok enough guff, any suggestions appreciated.

Thanks, Greg E

In reply to Efficient walk/iterate along a string by gje21c

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (3)
    As of 2017-11-19 07:06 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (278 votes). Check out past polls.