提高php的执行效率问题

在研究如何提高php的执行效率问题
一直觉着include过多的文件会提高php的执行时间

下面的资料表明,将数据缓存成php文件并不是最好的方式
在小数据量的情况下做文本缓存的可读性最好

另外说句 其实有些时候还是google好用
http://www.raditha.com/wiki/Readfile_vs_include

It is not often that you can write a PHP script that does not need to include the contents of different files as part of it’s output. If these includes happen to be php scripts themselves you have no choice but to use require or include.
However more often than not, the contents are static, usually html template component. With static includes you have many more options available.
aerwear
We will analyse some of these functions to find out which one is most suitable when dealing with files with static content. We use the term function loosely, because require and include are not real functions but language constructs.


Function Brief Description
string file_get_contents ( string filename [, int use_include_path]) Reads entire file into a string
int fpassthru ( resource handle) Output all remaining data on a file pointer
string fgets ( resource handle [, int length]) Gets line from file pointer
array file ( string filename [, int use_include_path]) Reads entire file into an array
require(string filename)
include(string filename)
require_once(string filename)
include_once(string filename)
includes and evaluates the specific file.
int readfile ( string filename [, int use_include_path]) Outputs a file

We will now attempt to ‘include’ the contents of a 1 megabyte file into the output produced by our php script. How you can generate files of specific sizes is described elsewhere. The execution times and peak memory consumption, as reported by xdebug have been tabulated below.

We compensate for file caching and background processes by executing each script 4 times and taking the average (mean) of the result number 2-4. The first result is always rejected. Any result that appears to be outlier is rejected. The mean is rounded to 5 decimal places.

 

Function Sample Usage Time (s) Memory (b)
file_get_contents echo file_get_contents($filename); 0.00564 1067856
fpassthru fpassthru($fp); 0.00184 20032
fgets
$fp = fopen($filename,"rb");
while(!feof($fp))
{
    echo fgets($fp);
}
0.07190 30768
file echo join(”",file($filename)); 0.06464 2185624
require_once require_once($filename); 0.08065 2067696
include include($filename); 0.08202 2067696
readfile readfile($filename); 0.00191 19208

What’s obvious from these results is that using fpassthru is far superior to all other methods. What’s not so obvious is that fpassthru and readfile are equally good. The fpassthru version runs 0.00007 seconds quicker than the readfile version. What that really means is that you need to run the script at least 100000 times to make significant saving. On memory consumption readfile seems to have use up around 1kb less than passthru. A kilo byte is a drop in the ocean for modern web servers with hundreds of megabytes if not gigabytes of memory.

The only conclusion that can be drawn from these studies is that fpassthru and readfile are equally good if you wish to include static content as part of the script’s output.

Before you rush off to change all your includes and requires into readfiles or fpassthrus let’s run the same test with a smaller (32Kb file). 32Kb is a more realistic size for an included file.

 

FunctionTime (s)Memory (b)
 32Kb File1Mb File32Kb File1Mb File
file_get_contents0.001520.00564524801067856
fpassthru0.001170.001842001620032
fgets0.001950.071903076030768
file0.001570.06464873442185624
require_once0.002250.08065679922067696
include0.002220.08202679282067624
readfile0.001170.001911919219208

readfile and fpassthru have once again tied for first place. This new set of results just confirms the fact that speed and scalability comes from your design and not from your code. The difference between the best performance and the worst is just 0.00108s too close to call.

The most significant feature of these results is that both fpassthru and readfile scale really well. In other words, memory consumption and execution time does not increase significantly with increase in file size. That does not always mean your script will be faster just because you use these functions instead of require or include.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值