Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Saving an array to a disk file

by meraxes (Friar)
on May 26, 2006 at 03:38 UTC ( #551760=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Saving an array to a disk file
Download Code
Re^3: Saving an array to a disk file
by GrandFather (Cardinal) 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

      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. :-)

      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.

      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 ($\,$,)?!?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-12-26 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls