note 91414 modified in function.file-get-contents by danbrown
| From: | danbrown@php.net | Date: | Tue, 09 Jun 2009 21:35:58 +0000 |
| Subject: | note 91414 modified in function.file-get-contents by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156672@lists.php.net to get a copy of this message | ||
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...
<?
$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.
--was--
In my dev environment with a relatively low-speed drive (standard SATA 7200RPM) reading a 25MB zip
file in 10 times...
<?
$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...
<?
$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.
http://php.net/manual/en/function.file-get-contents.php