Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Perl always reads in 4K chunks and writes in 1K chunks... Loads of IO!

by serf (Chaplain)
on Jan 01, 2006 at 15:47 UTC ( #520252=note: print w/ replies, xml ) Need Help??


in reply to Perl always reads in 4K chunks and writes in 1K chunks... Loads of IO!

If you are concerned about the performance while reading the file into an array I would recommend doing some sample code and running it against a large file (say 100MB or more) to test the speed difference between doing:

@array = <FILE>;
and doing:
while(<FILE>) { push(@array, $_); }
and run it multiple times to make sure you're not just getting the effect of the file being cached in memory.

I have found that while the version with the while loop *looks* longer it actually has always run faster in the tests I've done.

PS: a die message with your open statement like

open (DF, "test.txt") || die "Can't read 'test.txt': $!\n"
is your friend, as are:
use strict; use warnings;
at the top of your script - I'd recommend using them - they will help you by saving you time finding what's causing errors and in the long run should also help you to write better code by teaching you good habits.

:o)

update: Thanks ChOas - I've fixed it. I always use the () brackets and || myself - and vaguely recalled (like you point out) that there *was* a difference between || and 'or'.

After having Dominus do a presentation to us the other week and finding I am in th habit of using () where I don't absolutely need to, I'd thought I'd not add them here where NeilF wasn't already using them... I've put them back on now :o)

running:

perl -MO=Deparse -e 'open (DF, "test.txt") || die "Cant read test.txt\ +n";'
tells me I *could* write it:
die "Can't read test.txt\n" unless open DF, 'test.txt';
but I won't :o)


Comment on Re: Perl always reads in 4K chunks and writes in 1K chunks... Loads of IO!
Select or Download Code
Re^2: Perl always reads in 4K chunks and writes in 1K chunks... Loads of IO!
by ChOas (Curate) on Jan 01, 2006 at 16:48 UTC
    This:

    open DF, "test.txt" || die "Can't read 'test.txt': $!\n"

    Does not do what you think it does.

    The || ties itself to "test.txt", which is always true, and not to the return of the open.

    This:
    open(DF, "test.txt") || die "Can't read 'test.txt': $!\n"

    or:

    open DF, "test.txt" or die "Can't read 'test.txt': $!\n"

    (or binds less tight than ||)

    Would accomplish what you want.


    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      Why are you measuring under Windows to see what will happen on Unix?
      If you've only got one machine to play with, why not boot off a LiveCD (like Knoppix) and measure your code (or a key subset) under Linux?
      Might not be the same OS your ISP is using, but closer to Unix than Windows?
      Might make absolutely no difference, but at least you might be a bit closer to comparing apples to apples rather than apples (Unix) to oranges (Windows) ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-09-21 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (171 votes), past polls