edainworks.com :: VGR :: Comparing various file reading methods in PHP.


This is the source code used in the benchmark :

//
// comp_readfile.php : comparison of file reading methods
//
//VGR21062003 Création
//VGR15092009 ADDed code et conclusion
//

$files=array('16MB text file (HTML)'=>'test.txt','16MB binary file'=>'result1.dat');
foreach ($files as $descr=>$thefile) {
  echo"<h2>$descr</h2>";
  echo "filesize is ".filesize($thefile).'<br>'; //VGR22082009 ADDed

  echo "<hr>Classical way (VGR's Simplicity)<br>";
  TimerStart();
  // classical way (VGR's Simplicity)
  $DataArray =array(); // cleaner 
  if ( ! ( $fp = fopen($thefile, "r" )) ) echo "failure<br>";
  else {
    while ( $buffer = fgets( $fp, 128 ) ) { 
      $DataArray[]=$buffer;
    } // while not eof
    fclose($fp);
  } // if fopen
  TimerStop(TRUE);

  echo "<hr>Cirtap's Panacea<br>";
  TimerStart();
  // Cirtap's Panacea ("must be optimized")
  $DataArray =array(); // cleaner 
  if ( ! ( file_exists($thefile)) ) echo "failure<br>";
  else {
    $DataArray = file($thefile);
  }
  TimerStop(TRUE);

  echo "<hr>Classical way (into one big string)<br>";
  TimerStart();
  // classical way (VGR's Simplicity)
  $DataArray =array(); // cleaner 
  if ( ! ( $fp = fopen($thefile, "r" )) ) echo "failure<br>";
  else {
    while ( $buffer = fgets( $fp, 128 ) ) { 
      $DataArray[]=$buffer;
    } // while not eof
    fclose($fp);
  } // if fopen
  $DataArray=implode("", $DataArray); // one big string
  echo strlen($DataArray).' bytes read<BR>';
  TimerStop(TRUE);

  echo "<hr>Cirtap's Panacea (one big string)<br>";
  TimerStart();
  // Cirtap's Panacea ("must be optimized")
  $DataArray =array(); // cleaner 
  if ( ! ( file_exists($thefile)) ) echo "failure<br>";
  else {
    $DataArray = file($thefile);
    $DataArray=implode("", $DataArray); // one big string
  }
  echo strlen($DataArray).' bytes read<BR>';
  TimerStop(TRUE);

  echo "<hr>file_get_contents() (one big string)<br>";
  TimerStart();
  if ( ! ( file_exists($thefile)) ) echo "failure<br>";
  else {
    $DataArray=file_get_contents($thefile);
  }
  echo strlen($DataArray).' bytes read<BR>';
  TimerStop(TRUE);
} // for each file

Results :

16MB text file (HTML)
filesize is 17035685

Classical way (VGR's Simplicity)
page generated in 520.483 ms

Cirtap's Panacea
page generated in 716.793 ms

Classical way (into one big string)
17035685 bytes read
page generated in 1 s 121.606 ms

Cirtap's Panacea (one big string)
17035685 bytes read
page generated in 760.67 ms

file_get_contents() (one big string)
17035685 bytes read
page generated in 463.314 ms

16MB binary file
filesize is 16777200

Classical way (VGR's Simplicity)
page generated in 285.591 ms

Cirtap's Panacea
page generated in 520.78 ms

Classical way (into one big string)
16777200 bytes read
page generated in 766.996 ms

Cirtap's Panacea (one big string)
16777200 bytes read
page generated in 524.873 ms

file_get_contents() (one big string)
16777200 bytes read
page generated in 451.956 ms

done.

9. Comparaison / Benchmark top

FR

Il s'agit de savoir si file_get_contents() est la plus rapide des solutions... et si file() bat systématiquement une boucle à base de fgets()...
On a 16MB very simple binary file, reading file contents in an array, my fgets() loop beats file() by 46% ;
When putting all file's contents in a big string - thus comparing to file_get_contents(), recommended on php.net doocumentation -
then my fgets() loop is beaten by file() by 32% and by file_get_contents() by 42%
On a 16MB very text file, reading file contents in an array, my fgets() loop beats file() by 28% ;
When putting all file's contents in a big string,
then my fgets() loop is beaten by file() by 32% -again- and by file_get_contents() by 59% -clearly the best solution-

Evidemment, je lis toujours dans un tableau, avec fgets() ;-)

Cordialement,

EN

We want to know if it's true that file_get_contents() is the fastest solution... and if file() beats systematically an fgets()-based loop...
On a 16MB very simple ***binary*** file, reading file contents in an array, my fgets() loop beats file() by 46% ;
When putting all file's contents in a big string - thus comparing to file_get_contents(), recommended on php.net doocumentation -
then my fgets() loop is beaten by file() by 32% and by file_get_contents() by 42%
On a 16MB very simple ***text*** file, reading file contents in an array, my fgets() loop beats file() by 28% ;
When putting all file's contents in a big string,
then my fgets() loop is beaten by file() by 32% -again- and by file_get_contents() by 59% -clearly the best solution-

Needless to say, I almost always read file contents in an array ;-)) and I use fgets() ;-)
Best regards,


Execution follows...

16MB text file (HTML)

filesize is 17035685

Classical way (VGR's Simplicity)

page generated in 88.372 ms

Cirtap's Panacea

page generated in 44.309 ms

Classical way (into one big string)
17035685 bytes read

page generated in 98.857 ms

Cirtap's Panacea (one big string)
17035685 bytes read

page generated in 63.764 ms

file_get_contents() (one big string)
17035685 bytes read

page generated in 7.009 ms

16MB binary file

filesize is 16777200

Classical way (VGR's Simplicity)

page generated in 60.862 ms

Cirtap's Panacea

page generated in 23.164 ms

Classical way (into one big string)
16777200 bytes read

page generated in 42.804 ms

Cirtap's Panacea (one big string)
16777200 bytes read

page generated in 27.295 ms

file_get_contents() (one big string)
16777200 bytes read

page generated in 7.318 ms

Vincent Graux (VGR) for European Experts Exchange and Edaìn Works  back to list of test scripts
Last update 2013-09-12 08:37:33