Php can use the file function and fseek function to read large files, but the efficiency between the two may be different. This article introduces phpfile function and fseek function to achieve comparison and analysis of the reading efficiency of large files, for more information, see. Php can use the file function and fseek function to read large files, but the efficiency between the two may be different. This article introduces the comparison and analysis of the php file function and fseek function to achieve the reading efficiency of large files, for more information, see.
1. directly use the file function.
The file function reads all the content into the memory at a time. to prevent some poorly written programs from occupying too much memory, PHP causes insufficient system memory and server downtime, therefore, the maximum memory usage is 16 MB by default. set memory_limit = 16 M in ini. if this value is set to-1, the memory usage is not limited.
Below is a piece of code that uses file to retrieve the last line of the file:
It takes 116.9613 seconds to complete the code execution ).
My machine has 2 GB of memory. when I press F5 to run the machine, the system will be grayed out directly, and it will be restored after about 20 minutes. it can be seen that all such large files will be directly read into the memory, the consequence is serious, so no more than. memory_limit cannot be too high. Otherwise, you only need to call the data center to let the reset machine go.
2. directly use PHP fseek for file operations
This method is the most common method. it does not need to read all the content of the file, but directly uses pointers for operations, so the efficiency is quite efficient. When using fseek to operate files, there are also a variety of different methods, and the efficiency may be slightly different. below are two common methods:
Method 1
First, find the last EOF the file through fseek, then find the starting position of the last row, get the data of this row, find the starting position of the next row, and then take the location of this row, and so on until the $ num row is found.
# The implementation code is as follows:
0) { while ($t != "\n") { fseek($fp, $pos, SEEK_END); $t = fgetc($fp); $pos--; }// http://www.manongjc.com $t = " "; $data .= fgets($fp); $line--; } fclose($fp); echo $data ?>
It takes 0.0095 seconds to complete the code execution)
Method 2
The fseek method is used to read the last part of the file, but it is not a one-bit read, but a one-piece read. when each piece of data is read, put the read data in a buf, and then determine whether the last $ num row data has been read by changing the number of linefeeds (\ n.
# The implementation code is as follows:
$ Chunk )? $ Chunk: $ max-$ len; fseek ($ fp, ($ len + $ seekSize) *-1, SEEK_END); $ readData = fread ($ fp, $ seekSize ). $ readData; if (substr_count ($ readData, "\ n") >=$ num + 1) {// Author: Codefarm tutorial http://www.manongjc.com preg_match ("! (.*? \ N) {". ($ num)."} $! ", $ ReadData, $ match); $ data = $ match [0]; break ;}} fclose ($ fp); echo $ data;?>
It takes 0.0009 seconds to complete the code execution ).
Method 3
0); $pos = $n + 1; $lines = array(); while (count($lines) <= $n) { try { fseek($fp, -$pos, SEEK_END); } catch (Exception $e) { fseek(0); break; } $pos *= $base; while (!feof($fp)) { array_unshift($lines, fgets($fp)); } } return array_slice($lines, 0, $n); } var_dump(tail(fopen("access.log", "r+"), 10)); ?>
It takes 0.0003 seconds to complete the code execution)