note 30894 added to function.file-get-contents
| From: | dynom | Date: | Wed, 02 Apr 2003 13:23:36 +0000 |
| Subject: | note 30894 added to function.file-get-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46125@lists.php.net to get a copy of this message | ||
Speed depends on the file size and the function.
<?
function fopen_() {
$template_file = "file.php";
$fd = @fopen($template_file, "r");
$template_raw = "";
if ($fd) {
while (!feof($fd)) { $template_raw .= fgets($fd, 512); }
fclose ($fd);
return $template_raw;
}
}
function file_() {
$template_file = "file.php";
$template_raw = "";
if (file_exists($template_file)) {
return implode('',file($template_file));
}
}
?>
The file I use in these functions is smaller than 1k.
In this example file_() is faster then fopen_().
Runnig a benchmark with these functions resulted in:
file_() took me: 0.069 seconds running: 200x50 times
fopen_() took me: 0.152 seconds running: 200x50 times
ofcourse the time differs every time the script runs, but 8/10 times file_() beats fopen_().
So think about what you want with your script.
fopen() is not! alway's faster than file().
--
http://www.php.net/manual/en/function.file-get-contents.php
http://master.php.net/manage/user-notes.php?action=edit+30894
http://master.php.net/manage/user-notes.php?action=delete+30894
http://master.php.net/manage/user-notes.php?action=reject+30894