PHP uses the file function, fseek function to read large file efficiency comparative analysis _php tips

Source: Internet
Author: User

PHP read large files can use the file function and the fseek function, but there may be differences in efficiency, this article to introduce PHP file function and fseek function to achieve large file reading efficiency of comparative analysis, the need for friends can refer to.

1. Directly using the file function to operate

Because the file function is a one-time read all content into memory, and PHP in order to prevent some poorly written programs to occupy too much memory and cause the system is out of memory, so that the server is down, so by default, limit the maximum use of memory 16M, this is through the php.ini Memory_ Limit = 16M To set, if the value is set to 1, then the amount of memory is unrestricted.

Here is a section of code that uses file to remove the last line of the document:

<?php
  ini_set (' memory_limit ', '-1 ');
  $file = ' Access.log ';
  $data = file ($file);
  $line = $data [Count ($data)-1];
  echo $line;
  ? >

Complete code Execution time consuming 116.9613 (s).

My machine is 2 G of RAM, when pressed F5 run, the system directly dimmed, almost 20 minutes later to recover, visible will be so large files directly into the memory, the consequences of how much serious, so not million, memory_limit this thing can not be too high, otherwise only call the room, Let's reset the machine.

2. Direct use of PHP fseek for file operation

This is the most common way, it does not need to read the contents of the file into the content, but directly through the pointer to operate, so efficiency is quite efficient. There are a number of different ways in which you can use fseek to manipulate a file, and the efficiency may vary slightly, and here are two common methods:

Method One

First through the fseek find the last EOF of the file, then find the starting position of the last line, take this row of data, find the first line of the starting position, and then take the position of the line, and so on until the $num line is found.

The implementation code is as follows


<?php
  $fp = fopen ($file, "R");
  $line = ten;
  $pos =-2;
  $t = "";
  $data = "";
  while ($line > 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
  ?>

Complete code Execution time consuming 0.0095 (s)

Method Two

Or in a fseek way from the end of the file to read, but this is not a read, but a piece of reading, each reading a piece of data, the read data in a buf, and then through the number of line breaks (\ n) to determine whether the last $num row of data has been read.

The implementation code is as follows

<?php
  $fp = fopen ($file, "R");
  $num = ten;
  $chunk = 4096;
  $fs = sprintf ("%u", FileSize ($file));
  $max = (Intval ($fs) = = Php_int_max)? Php_int_max:filesize ($file);
  for ($len = 0; $len < $max; $len + = $chunk)
  {
    $seekSize = ($max-$len > $chunk)? $chunk: $max-$len; 
   
    fseek ($fp, ($len + $seekSize) *-1, seek_end);
    $readData = Fread ($fp, $seekSize). $readData;
    if (Substr_count ($readData, "\ n") >= $num + 1)
    {
      //Author: Code Agriculture tutorial  http://www.manongjc.com
      Preg_match ("! (. *?\n) {" . ($num). "}$!", $readData, $match);
      $data = $match [0];
      break;
    }
  }
  Fclose ($FP);
  echo $data;
  ? >

   

Complete code Execution time consuming 0.0009 (s).

Method Three

<?php
  function Tail ($fp, $n, $base = 5)
  {
    assert ($n > 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+"));
  >

Complete code Execution time consuming 0.0003 (s)

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.