Readfile vs include 性能比较
来源:互联网 发布:迈越网络 退款 编辑:程序博客网 时间:2024/06/06 05:16
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.
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.
$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.
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.
- Readfile vs include 性能比较
- PHP各种引入(读取)文件函数的执行效率include require fpassthru readfile等的性能比较
- PHP autoload与include性能比较
- PHP autoload与include性能比较
- PHP autoload与include性能比较
- PHP autoload与include性能比较
- PHP 下载 file_put_contents vs readfile
- readFile
- ReadFile
- ReadFile
- ReadFile
- ReadFile()
- ReadFile
- ReadFile
- ReadFile
- NoSQL数据库性能比较:MongoDB vs Redis vs Tokyo Tyrant
- NoSQL数据库性能比较:MongoDB vs Redis vs Tokyo Tyrant
- lighttpd VS ngix 性能比较,技术架构比较
- 微软发布XNA游戏专业级编程模版——极品飞车!
- 关于swing线程的例子
- 使用Microsoft Live Writer离线写blog
- 使用ASP.NET跟踪Web应用程序
- Use Array
- Readfile vs include 性能比较
- HTML中小meta的大作用
- 中文字符集与字符编码的基础知识
- 盛夏的果实
- 汇总c#.net常用函数和方法集 (转)
- 排序算法集合
- 语言学习
- 10大Ubuntu应用程序让你的电脑起飞
- 内存管理内幕--动态分配的选择、折衷和实现(二)