saranperl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: read huge file
by moritz (Cardinal) on Aug 07, 2009 at 11:51 UTC

    You asked about read and sysread already, so you know about other ways.

    There are two possibilities: either the parsing code is too slow (you can find that out by profiling, as was previously suggested to you), or it's really the IO that's slow. If that's the case, using a different method to read the file won't speed it up significantly. All you can do is obtaining a faster system.

    Update: on my system it takes about 3.2 seconds to read 1GB with an ordinary while (<$handle>) loop, and about 1.4 seconds to read it with sysread. (The file might be still in the IO buffers of my operating system). So maybe it's really your parsing code that's being slow, not the IO?

    See also: reading file. Repeating your questions (and phrasing them slightly different) won't help you at all.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: read huge file
by SuicideJunkie (Vicar) on Aug 07, 2009 at 14:03 UTC

    Try benchmarking your code to see if that really is the problem, first.


    If that is indeed where the slowdown occurs, consider reading in more manageable chunks by adjusting the input record separator. If a line is extremely long, you can run into trouble reallocating slowly up to 35 megs of ram, a few k at a time. If your records are still large, but of a known size, you can pre-allocate your storage variable to fatten it up for the task.

      I fear you lost him at 'benchmarking' :-/

      -- Time flies when you don't know what you're doing
Re: read huge file
by ig (Vicar) on Aug 07, 2009 at 12:32 UTC

    You could try Mmap or perhaps one of the Tie modules. Or, to completely avoid Perl I/O overhead, you could write an XS module and use your choice of system functions directly: read, mmap or whatever works best in your context.

Re: read huge file
by salva (Canon) on Aug 07, 2009 at 12:28 UTC
    unless you give us a full description of your problem (including how is the data in the file and what are you doing with it) we may not be able to help you!