note 91414 deleted from function.file-get-contents by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:37:36 +0000 |
| Subject: | note 91414 deleted from function.file-get-contents by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215227@lists.php.net to get a copy of this message | ||
Note Submitter: corey at effim dot com
----
In my dev environment with a relatively low-speed drive (standard SATA 7200RPM) reading a 25MB zip
file in 10 times...
<?php
$data =
cat /tmp/test.zip;
// 1.05 seconds
$fh = fopen('/tmp/test.zip', 'r');
$data = fread($fh, filesize('/tmp/test.zip'));
fclose($fh);
// 1.31 seconds
$data = file_get_contents('/tmp/test.zip');
// 1.33 seconds
?>
However, on a 21k text file running 100 iterations...
<?php
$data = cat /tmp/test.txt;
// 1.98 seconds
$fh = fopen('/tmp/test.txt', 'r');
$data = fread($fh, filesize('/tmp/test.txt'));
fclose($fh);
// 0.00082 seconds
$data = file_get_contents('/tmp/test.txt');
// 0.0069 seconds
?>
Despite the comment about file_get_contents being faster do to memory mapping, file_get_contents is
slowest in both of the above examples. If you need the best performance out of your production box,
you might want to throw together a script to check out which method is fastest for what size files
on that particular machine, then optimize your code to check the file size and use the appropriate
function for it.