I didn't set out to find this. I just happened to pass an substr ref into an existing subroutine that takes a scalar ref argument and uses substr to iterate over the data pointed at by that reference in chunks.

It was only when that subroutine that would normally take a couple of seconds to run, was still running 40 minutes later that I went looking for the reason. The effect was so dramatic that I thought it worth looking at.

The subroutine in question is one that takes large genome sequences and prints them in FASTA format (wrapped every 50 chars). The input scalars can be 100s of megabytes in length, hence the reason for passing a reference rather than a copy. Breaking such large scalars into a list using split or unpack would dramatically increase memory usage, hence the reason for iterating over the scalar using substr.

The subroutine has worked well for several years, but on this occasion I was just generating (simulating) a very large FASTA file for testing some ideas. So, instead of generating a new large scalar of random ACGTs for each record, I generated a single random sequence of the largest size possible, and was then selecting a substring of that for output as each record. It was when passing those substring refs to the FASTA output routine that I encountered the slowness.

As I was trying to generate a 3.5GB FASTA test file, something that under normal circumstances might take a couple of hours, with this combination of factors and the 2000x slowdown, it would have taken almost 6 months!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^2: Access via substr refs 2000 times slower by BrowserUk
in thread Access via substr refs 2000 times slower by BrowserUk

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!
  • 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, details, 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, summary, 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.