http://www.perlmonks.org?node_id=551760


in reply to Re: Saving an array to a disk file
in thread Saving an array to a disk file

Just a quick question because I don't know if perl would optimize that code but... would the join not be a bit onerous if the array is very large? (Also, cleaned up teh code a little.)

Perhaps a foreach loop?

use strict; use warnings; my @array = ('array','stuff','here'); open OUTFILE, '>', 'filename' or die "Couldn't create filename: $!"; print OUTFILE $_,"\n" foreach @array; close OUTFILE;

Update: D'oh. Cleaned up my comment a little... would help if I could type

Replies are listed 'Best First'.
Re^3: Saving an array to a disk file
by GrandFather (Saint) on May 26, 2006 at 03:55 UTC

    Hard to be sure. The join generates a large string that is output as one large write (maybe). The foreach (possibly) generates a large number of small writes. For file I/O one large write (the join) generally is much better than a large number of smaller writes (the foreach).

    On the other hand, if the string gets so large it starts swapping out to disk, then the join is likely to be much worse.


    DWIM is Perl's answer to Gödel

      Are you not forgetting about IO buffering in the writing example? I expect the file layer to write one block at a time, regardless of how large or small each individual print is, unless you've disabled buffering.

      I hadn't thought of the file I/O issue. Very good point.

      When I started using Perl I was munging 1Gb files so I was assuming that the array was very big. :-)

      Hard to be sure. The join generates a large string that is output as one large write (maybe). The foreach (possibly) generates a large number of small writes. For file I/O one large write (the join) generally is much better than a large number of smaller writes (the foreach).

      On the other hand, if the string gets so large it starts swapping out to disk, then the join is likely to be much worse.

      And how 'bout good 'ol ($\,$,)?!?