I have been writing output from perl scripts to files for some time using the following code:
open( OUTPUT, ">:utf8", $output_file ) or die "Can't write new file: $!"; print OUTPUT "First line I want printed\n"; print OUTPUT "Another line I want printing\n"; close(OUTPUT);
It works faster than my original approach, which used "say" instead of printing (thanks NYTProf for enlightening me!)
However, my current script iterates through hundreds of thousands of lines and takes many hours to run this method, and NYTProf points my finger at my thousands of print commands. So the question is ... Is there a faster way to do this?
Other information that may be relevant ... Perl Version: 5.14.2 (On Ubuntu)
Background script ... Row '|' delimited flat files are read in hashes; each file has some primary keys that map records from one to another. I manipulate this data and they combine them into one file for import into another system.
The output file is about 3 million lines, and the program starts to slow down noticeably after writing about 30,000 lines to the specified file. (A little reading seemed to indicate that you had exhausted the write buffer in other languages, but I could not find anything about this regarding perl?)
EDIT: Now I tried adding the line below, right after the open () statement, to disable print spooling, but the program still slows down around the 30,000th line.
OUTPUT->autoflush(1);
source share