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

I'm running Perl source that references an external file against a userid for information about that individual. Currently I'm continually opening the file and searching for a particular userid which is chewing up I/O - what would be a more intelligent means of doing the lookup and thus cut down the run time (hopefully)....thanks

Replies are listed 'Best First'.
Re: external file reference
by moritz (Cardinal) on Oct 28, 2009 at 16:17 UTC
    Use a database, which does the IO intelligently for you.
Re: external file reference
by Perlbotics (Archbishop) on Oct 28, 2009 at 16:20 UTC

    If I understand your question correct, then you are scanning a file that is continually updated/growing - like a log file. One approach to reduce CPU- and I/O-load is to open the file once and then scan only the lines that were appended. Maybe File::Tail might suit your intention?

      ..actually, the file I'm scanning is static (one that I created with Perl code) but contains 14K lines!

        14k lines of normal line sizes of maybe 100 chars isn't that bad. Consider reading the file into something like $hash{userid}.

        which Keszler already said while I as usual did forget to check for new replies before create

        Then, if you find the hash build time being significant wrt total runtime, while your 14k file is fairly static: consider a lightweight database like sqlite accessed via DBI.

        If you've many independent script or parallel script runs, keeping the hash in memory in a separate process might also be worthwile (old-style client server or some shared memory setup - but that's somewhat like a coding challenge looking for a problem).

        cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.
        So, taking a worst-case scenario of a 15,000 line file averaging 200 bytes/line, that's 3MB. Store it all in a hash structure and there'll be some overhead, but even if the program took 4MB RAM total that's not very significant on today's boxes.

        OTOH, if you're running this on a PII-266 w/ 64MB RAM it'd be a bit much.