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

First edition 21/06/2003
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 78.738 ms

Cirtap's Panacea

page generated in 41.491 ms

Classical way (into one big string)
17035685 bytes read

page generated in 95.27 ms

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

page generated in 59.803 ms

file_get_contents() (one big string)
17035685 bytes read

page generated in 5.917 ms

16MB binary file

filesize is 16777200

Classical way (VGR's Simplicity)

page generated in 56.285 ms

Cirtap's Panacea

page generated in 20.232 ms

Classical way (into one big string)
16777200 bytes read

page generated in 39.982 ms

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

page generated in 22.832 ms

file_get_contents() (one big string)
16777200 bytes read

page generated in 5.927 ms

Vincent Graux (VGR) for European Experts Exchange and Edaìn Works  back to list of test scripts
Last update 2024-05-10 14:07:07