2010-02-24 43 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