|XP is just a number|
Re: Slurping file into array VS Process file line-by-lineby jarich (Curate)
|on Oct 27, 2004 at 09:26 UTC||Need Help??|
Corion and eyespoplikeamosquito have discussed your specific code examples, so I'll discuss the general question:
The short answer is that you should always process files line by line unless you can't otherwise avoid it.
Times when you might not be able to avoid it are when you wish to do some sort of processing on the whole file, for example, sorting all the lines (although there's usually better tools for this) or picking out a random quote (although there are better tools for this too).
If you don't have to be able to access all the lines of a file in a semi-random order then you should always process it line by line.
Why? Because one day someone will feed your program a very large file. Perhaps one which is 10 Gb big. If you process that line by line your memory footprint will be comparatively small, if you slurp it all into memory, then your memory footprint will be *at least* as big as the file, sometimes almost twice that, and your machine might come grinding to a halt.
The pro of processing a file line by line is that your program stays fast and usable. The con is that you can only refer to lines you've already seen (if you've stored them) and the current lines. Lines further into the file are much more difficult to access. Fortunately you don't usually have to.
The pro of slurping a whole file into memory is that you can access any line of it easily. The con is that you might need a lot of memory, and it can really slow your machine down.
Hope that helps.