Well, first of all your pre-extending is not doing what you think. When you pre-extend an array, you fill it with undefs. When you push onto that, you are then adding to the end of the array, after the undefs. You need to insert by index.

Personally I am inclined to forget about pre-extending. The doubling logic that Perl uses for allocating memory means that in building a large array it only moves pointers an average of once each. Adding extra logic in Perl to avoid this expense can't save much, and usually loses.

Based on both theory and experiments that I just ran, your code should not have any significant algorithmic inefficiencies unless your data is slow. Given the overhead of Perl data structures, the amount of data, RAM, etc that you have, and your described performance profile I am fairly confident that your performance problem is memory pressure. I don't know the details of how Windows XP handles memory, but it would not surprise me in the least for it to either have complications in how it reports memory that mislead you, or for it to choose to page memory earlier than you think.

My strong advice is to rework this script so that you don't need to have all of this data in RAM at once. (Without seeing the rest of the script I can't give good advice on how to do that.) Similarly you can save a big chunk of RAM by not storing the file in an array, process it as you read it instead.


In reply to Re: Slowness when inserting into pre-extended array by tilly
in thread Slowness when inserting into pre-extended array by ryangabbard

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.