reading a block of lines in a file using php

后端 未结 5 1499
旧时难觅i
旧时难觅i 2020-12-20 03:16

Considering i have a 100GB txt file containing millions of lines of text. How could i read this text file by block of lines using PHP?

i can\'t use file_get_

5条回答
  •  一整个雨季
    2020-12-20 03:52

    I know this is an old question, but I think there is value for a new answer for anyone that finds this question eventually.

    I agree that reading 100GB takes time, that I why I also agree that we need to find the most effective option to read it so it can be as little as possible instead of just thinking "who cares how much it is if is already a lot", so, lets find out our lowest time possible.

    Another solution:

    Cache a chunk of raw data

    Use fread to read a cache of that data

    Read line by line

    Read line by line from the cache until end of cache or end of data found

    Read next chunk and repeat

    Grab the un processed last part of the chunk (the one you were looking for the line delimiter) and move it at the front, then reads a chunk of the size you had defined minus the size of the unprocessed data and put it just after that un processed chunk, then, there you go, you have a new complete chunk.
    Repeat the read by line and this process until the file is read completely.

    You should use a cache chunk bigger than any expected size of line.

    The bigger the cache size the faster you read, but the more memory you use.

提交回复
热议问题