2010-02-24 90 views
15

有一個文件緩存系統,用於我經常使用的php 5庫。 當請求時,我檢查緩存的文件,如果有一個我渲染並退出。file_get_contents比include慢嗎?

$contents = file_get_contents(self::_cacheFile()); 
echo $contents; 
exit();  

我必須做的file_get_contents,而不是僅僅包括因爲緩存的XML文件與討厭的

`<?xml version="1.0"?>` 

有沒有更好的辦法在我的緩存文件拉不shorttags射擊?

+2

而且更快,更內存佔用那麼人們說短標籤沒有問題... –

+0

是的,我討厭shortags,似乎太容易觸發像意外的PHP上面的例子。 –

+0

這對於二進制存儲而言非常尖銳。如何壓縮文件進行存儲並將其解壓縮到內存中?或者,如果您正在緩存整個響應,則發送壓縮副本?這將避免短標籤。就我個人而言,當我到達php.ini時,我會關閉短標籤。 – TheJacobTaylor

回答

10

如果您只想輸出文件內容,則應使用readfile()。這比file_get_contents()函數

31

由於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 
11

file_get_contentsinclude不會做同樣的事情:

  • file_get_contents讀取文件的內容,並返回一個字符串
  • include將執行該文件的內容。

關於速度,沒有一個操作碼緩存,我想file_get_contents理論上應該是更快,因爲它較少計算(代碼沒有編譯/執行)

不過,最重要的可能是你想要做的事情:如果你只想讀一個文件,你應該使用file_get_contents

+0

但是稍微有一點,因爲文件中沒有代碼...... –

10

沒有什麼比一個(做得好)的基準測試(這比看起來更難,我可能忽略了一些東西)。儘管兩者都是在相同條件下製成的,但它們應該成爲衡量標準。

test.txt的是12KB,876行的文本文件:

[email protected]:~$ ls -la test.txt ; wc -l test.txt 
-rw-r--r-- 1 vinko vinko 12264 2010-02-24 19:08 test.txt 
876 test.txt 

file_get_contents.php:

[email protected]:~$ more file_get_contents.php 
<?php 
echo file_get_contents("test.txt"); 
?> 

include.php

[email protected]:~$ more include.php 
<?php 
include("test.txt"); 
?> 

readfile.php

[email protected]:~$ more readfile.php 
<?php 
readfile("test.txt"); 
?> 

所以,我們計時爲10萬次每次執行:

[email protected]:~$ time for i in `seq 10000`; do php file_get_contents.php >/dev/null; done 

real 3m57.895s 
user 2m35.380s 
sys  1m15.080s 

[email protected]:~$ time for i in `seq 10000`; do php include.php >/dev/null; done 

real 3m57.919s 
user 2m37.040s 
sys  1m16.780s 

vink[email protected]:~$ time for i in `seq 10000`; do php readfile.php >/dev/null; done 
real 3m57.620s 
user 2m38.400s 
sys  1m14.100s 

結論:所有這三個實際上是相當於對PHP 5.2.4用了Suhosin小塊12個KB的文本文件。

+1

應該是'echo $ contents;'沒有'()'。 'readfile'直接寫入輸出緩衝區並返回一個整數,所以不需要回顯。只需調用'readfile'。你也不需要'退出'調用。 – Gordon

+0

@戈登:謝謝,改變了腳本並重新測試。更加相同:) –

1

的file_get_contents將檢索幾個原因緩存文件的最快方法:

  1. 它完成它加載的數據沒有處理(解析PHP代碼,處理換行符等)
  2. 它使用優化的技術儘可能快地加載文件(內存映射文件爲一個)。
5

謝謝你的提示,對於那些誰是好奇

readfile(); 
<!-- dynamic page rendered in 0.133193016052 seconds.--> 
<!-- static page rendered in 0.00292587280273 seconds.--> 

file_get_contents(); 
<!-- dynamic page rendered in 0.133193016052 seconds.--> 
<!-- static page rendered in 0.00303602218628 seconds.--> 

include(); 
<!-- dynamic page rendered in 0.133193016052 seconds.--> 
<!-- static page rendered in 0.00348496437073 seconds.--> 
+5

對於基準測試而言,單次執行是不夠的,您必須迭代很多 –

+1

靜態頁面的響應時間延長了10%。現在,當你打開一個操作碼緩存時會發生什麼? +1進行研究。 – TheJacobTaylor