Gossamer Forum
Home : General : Perl Programming :

'WHILE' SPEED QUESTION

Quote Reply
'WHILE' SPEED QUESTION
which is the best method *in speed* to open and read a text file.

1. open(FILE); @file=<FILE>; close(FILE) # then work on @file
2. open(FILE); while (<FILE>) { #do whatever...
Quote Reply
Re: [robyone] 'WHILE' SPEED QUESTION In reply to
@file=<FILE> will read the whole file into memory and store it in an array, so I wouldn't do this unless you really wanted the file's contents to be in an array. This one's especially bad if the file is big (as it reads the whole thing into memory). The while loop is much more efficient with memory. Generally, use the while loop when you want to do something with every line in a file.

Adrian
Quote Reply
Re: [robyone] 'WHILE' SPEED QUESTION In reply to
The second method will be faster since you only have a single line of a file stored in memory at a time.

--Philip
Links 2.0 moderator
Quote Reply
Re: [robyone] 'WHILE' SPEED QUESTION In reply to
....and if you are finished what you are doing in a loop or on a particular line use last; or next;