39
13
|
I have just found out that my script gives me a fatal error:
That line is this:
So I think it is having difficulty loading the file into memeory and counting the number of lines, is there a more efficient way I can do this without having memory issues?
The text files that I need to count the number of lines for range from 2MB to 500MB. Maybe a Gig sometimes.
Thanks all for any help.
| ||
add a comment
|
79
|
This will use less memory, since it doesn't load the whole file into memory:
fgets loads a single line into memory (if the second argument $length is omitted it will keep reading from the stream until it reaches the end of the line, which is what we want). This is still unlikely to be as quick as using something other than PHP, if you care about wall time as well as memory usage.
The only danger with this is if any lines are particularly long (what if you encounter a 2GB file without line breaks?). In which case you're better off doing slurping it in in chunks, and counting end-of-line characters:
| |||
PHP_EOL
- does that look right? – Dominic Rodger Jan 29 '10 at 14:58\n
) being parsed on a windows machine (PHP_EOL == '\r\n'
) – nickf Jan 29 '10 at 15:01fgets
work? – Dominic Rodger Jan 29 '10 at 15:23