Are there other techniques I should use to traverse a large file like this and which might offer methods to move forward, back, go to beginning, etc.?

See seek. It works best if the records are fixed length. If they are not then creating an index that maps record number to file position is very simple and makes for quite fast access.

I have a file that is a 3.6GB and contains 40e6 records. I index it like this:

perl -e"BEGIN{binmode STDOUT}" -ne"print pack'Q',tell STDIN" syssort >syssort.idx

Which takes just a couple of minutes to run. I can then randomly access the records in that file using:

#! perl -slw use strict; use Time::HiRes qw[ time ]; our $N //= 1000; open IDX, '+<:raw', 'syssort.idx' or die $!; open DAT, '+<:raw', 'syssort' or die $!; my $start = time; for ( 1 .. $N ) { my $recnum = int rand 40e6; seek IDX, $recnum *8, 0; my $idx; read IDX, $idx, 8; my $pos = unpack 'Q', $idx; seek DAT, $pos, 0; chomp( my $record = <DAT> ); # printf "Record %d: '%s'\n", $recnum, $record; } my $elapsed = time - $start; printf "$N random records read in %.3f seconds (%6f/s)\n", $elapsed, $elapsed / $N; __END__ c:\test>syssort-idx -N=1e4 1e4 random records read in 2.223 seconds (0.000222/s) c:\test>syssort-idx -N=1e5 1e5 random records read in 21.332 seconds (0.000213/s) c:\test>syssort-idx -N=1e3 1e3 random records read in 0.218 seconds (0.000218/s) c:\test>syssort-idx -N=1e3 1e3 random records read in 0.226 seconds (0.000226/s)

At 0.2 milliseconds per record, it is fast enough for most purposes.


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.
RIP an inspiration; A true Folk's Guy

In reply to Re: help reading from large file needed by BrowserUk
in thread help reading from large file needed by bigtiny

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.