Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Speed of opening and closing a file 1000s times

by hanenkamp (Pilgrim)
on Nov 03, 2003 at 17:00 UTC ( [id://304158]=note: print w/replies, xml ) Need Help??


in reply to Speed of opening and closing a file 1000s times

I would suggest neither solution. Why not simply keep all the files open? Or if there are many, you could keep a cache of open files which closes files based upon how long ago they were used (an LRU cache) or some other kind of heuristic that can determine the likelihood the file will be used again soon (such as frequency of use). For example:

my %file_cache; sub open_file($) { my $filename = shift; return $file_cache{$filename} || (open $file_cache{$filename}, ">>$filename" or die "blah blah $!"); } while (<FILE> { foreach my $n (@natures) { if ($_ =~ /%n/) { my $out = open_file "$n.txt"; print $out $n; } } }

If there is the potential for many files, delete an entry from the cache managed by open_file when the given $filename isn't currently open and the cache is "full".

This way you don't have to open files over and over again quite so often--we hope--and you don't have to perform reiterations.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://304158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2025-01-13 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (31 votes). Check out past polls.