http://www.perlmonks.org?node_id=275921


in reply to Slowness when inserting into pre-extended array

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.

  • Comment on Re: Slowness when inserting into pre-extended array

Replies are listed 'Best First'.
Re: Re: Slowness when inserting into pre-extended array
by liz (Monsignor) on Jul 19, 2003 at 23:49 UTC
    Some remarks on the code in the while loop:
    $line=~/^(\S*) [0-9.]* (.*)$/o;
    The /o modifier is not necessary, as the string to match is already a constant and is therefor already compiled at compile time. From perldoc perlop:

    PATTERN may contain variables, which will be interpolated (and the pattern recompiled) every time the pattern search is evaluated, except for when the delimiter is a single quote. (Note that $(, $), and $| are not interpolated because they look like end-of-string tests.) If you want such a pattern to be compiled only once, add a "/o" after the trailing delimiter. This avoids expensive run-time recompilations, and is useful when the value you are interpolating won't change over the life of the script. However, mentioning "/o" constitutes a promise that you won't change the variables in the pattern. If you change them, Perl won't even notice.

    my ($class, $feature_vector) = ($1, $2);
    Are you sure that the regex will always match? If not, you may introduce duplicates for each time there's no match. From perldoc perlre:

    NOTE: failed matches in Perl do not reset the match variables, which makes easier to write code that tests for a series of more specific cases and remembers the best match.

    Liz

      re: /o
      Thanks!
      re: will regexp always match?
      Yes - it's taken directly from some other code that reads the same file format with no problem.
        How do you know that the other code didn't have silent errors on 5 lines out of 300,000? It is free to put a check that will blow up if you fail to match, and being in the habit of testing implicit assumptions is never a bad thing.