设为首页 收藏本站
查看: 786|回复: 0

[经验分享] PHP各种引入文件函数的执行效率include require fpassthru readfile

[复制链接]

尚未签到

发表于 2015-8-26 15:38:07 | 显示全部楼层 |阅读模式
http://www.ccvita.com/163.html
  在研究如何提高php的执行效率问题
一直觉着include过多的文件会提高php的执行时间
但是又时候一些文件确实又需要去引入,尝试着去试验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.

  



Function
Time (s)
Memory (b)




32Kb File
1Mb File
32Kb File
1Mb File


file_get_contents
0.00152
0.00564
52480
1067856


fpassthru
0.00117
0.00184
20016
20032


fgets
0.00195
0.07190
30760
30768


file
0.00157
0.06464
87344
2185624


require_once
0.00225
0.08065
67992
2067696


include
0.00222
0.08202
67928
2067624


readfile
0.00117
0.00191
19192
19208
  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.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104603-1-1.html 上篇帖子: [PHP]windows下手动安装phpunit 下篇帖子: PHP代码优化技巧大盘点
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表