in reply to Intelligent logging
Overall, however, opening and closing files is generally O(1), which means they generally are rather fast. Now, reading and writing data can be more costly. But, the cost is usually in the memory structures people read into than in the actual cost of reading. Writing, especially appending as in a logfile is almost O(0); in other words, it's so negligible as to be irrelevant.
Now, my question is why are you closing the file after every iteration? It sounds like you really only want to flush the buffers after every iteration. So, you could do something like:
use IO::File; my $fh = IO::File->new( ">>$filename" ) or die "Cannot open '$filename +' for appending: $!\n"; # Do your stuff here. At the end of every iteration, call $fh->flush; $fh->close;
Now, an alternative would be to turn autoflushing on, using $|++; at the top of your script, preferably in a BEGIN block. There are many developers, including merlyn and Abigail-II that do that reflexively. I didn't suggest that at first for a few reasons:
You just have to figure out what's best for your specific need.
Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Intelligent logging
by chromatic (Archbishop) on Oct 29, 2004 at 01:37 UTC | |
|
Re^2: Intelligent logging
by thor (Priest) on Jul 14, 2005 at 15:27 UTC |