Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

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

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

GVGFTVILISFYVGFFYNVIIAWALHYFFSSFTMDLPWIHCNNTWNSPNCSDAHASNSSDGLGLNDTFGTTPAAEYFER.

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (6)
    As of 2014-07-13 14:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (249 votes), past polls