由於include
將評估文件的內容,例如,通過PHP解釋器運行,並使用include_path查找文件,我會說include
更慢。 file_get_contents
只會將文件的內容視爲字符串。更少的開銷,更快的速度。
從manual page:
file_get_contents()函數是要讀取的文件的內容轉換成字符串的首選方式。如果您的操作系統支持,它將使用內存映射技術來提高性能。
但是,如果你是輸出的,而不是得到它轉換成字符串的文件後,readfile()
即使是一點點比file_get_contents
更快。鑑於include
'ing也會輸出任何非PHP內容,這可能更可能是我猜測後的結果。
我的臺式機上
修訂基準:
$start1 = microtime(1);
for($i=0; $i<100000; $i++) {
include 'log.txt';
}
$end1 = microtime(1) - $start1;
和
$start2 = microtime(1);
for($i=0; $i<100000; $i++) {
echo file_get_contents('log.txt');
}
$end2 = microtime(1) - $start2;
和
$start3 = microtime(1);
for($i=0; $i<100000; $i++) {
readfile('log.txt');
}
$end3 = microtime(1) - $start3;
結果
echo PHP_EOL, $end1, // 137.577358961
PHP_EOL, $end2, // 136.229552984
PHP_EOL, $end3; // 136.849179029
而且更快,更內存佔用那麼人們說短標籤沒有問題... –
是的,我討厭shortags,似乎太容易觸發像意外的PHP上面的例子。 –
這對於二進制存儲而言非常尖銳。如何壓縮文件進行存儲並將其解壓縮到內存中?或者,如果您正在緩存整個響應,則發送壓縮副本?這將避免短標籤。就我個人而言,當我到達php.ini時,我會關閉短標籤。 – TheJacobTaylor