0

I am trying to read a text file with 7 million lines hereby named HUGE. I am trying to read the first 10 thousand lines and then delete them. I am presently using SplFileObject like this...

    $file = new SplFileObject(FILENAME);
    for ($i = $start; $i <= $end; $i++) {
        $file->seek($i);
        $lines .= $file->current();
    }
    return($lines);

I do not yet have HUGE. I need to know if SplFileObject can load such a huge file. I need to calculate the length of HUGE every time I remove lines from it. "file" seems promising since count(array_HUGE) will give me the length immediately. I need to make a design decision now. Which of the 2 has a smaller footprint and is faster in loading HUGE?

Mallik Kumar
  • 540
  • 1
  • 5
  • 28
  • It does not matter. The one is just a nice "wrapper" over the other. – zerkms Dec 19 '14 at 03:27
  • So I can use file to load a 7 million lines file? Does file hold all the lines in memory or only partially? – Mallik Kumar Dec 19 '14 at 03:29
  • Oops, you've mentioned `file()` function, confused it with `fopen()` (which would make more sense for this question). Then forget what I said, `file()` reads and allocates memory for all the contents. – zerkms Dec 19 '14 at 03:30
  • But I would have to loop over the whole file using fopen() second time to know the number of lines. That is a performance hit. – Mallik Kumar Dec 19 '14 at 03:33
  • That's right, you need to loop over it. That's how you read files. – zerkms Dec 19 '14 at 03:33
  • And for inspiration - check how GNU `wc` is implemented: https://github.com/goj/coreutils/blob/rm-d/src/wc.c – zerkms Dec 19 '14 at 03:38

0 Answers0