PHP性能file_get_contents()vs readfile()和cat [英] PHP performance file_get_contents() vs readfile() and cat

查看:126
本文介绍了PHP性能file_get_contents()vs readfile()和cat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP文件读取功能进行一些基准测试,仅仅是为了我的整体知识。
所以我测试了三种不同的方式来读取我认为非常快的文件的整个内容。

I am doing some benchmarking with PHP file reading functions just for my overall knowledge. So I tested three different ways to read the whole content of a file that I thought would be very fast.


  • file_get_contents(知道其非常高的性能

  • readfile()在将数据直接输出到 stdout时,已知是file_get_contents()的一个非常好的替代品。

  • exec('cat filename')一个非常方便快捷的UNIX命令

所以这是我的基准测试代码,请注意我为 readfile()启用了PHP缓存系统,以避免直接输出完全伪造结果。

So here is my benchmarking code, note that I enabled the PHP cache system for readfile() to avoid the direct output that would totally falsify the results.

<?php
/* Using a quick PNG file to benchmark with a big file */

/* file_get_contents() benchmark */
$start = microtime(true);
$foo = file_get_contents("bla.png");
$end = microtime(true) - $start;
echo "file_get_contents() time: " . $end . "s\n";

/* readfile() benchmark */
ob_start();
$start = microtime(true);
readfile('bla.png');
$end = microtime(true) - $start;
ob_end_clean();
echo "readfile() time: " . $end . "s\n";

/* exec('cat') benchmark */
$start = microtime(true);
$bar = exec('cat bla.png');
$end = microtime(true) - $start;
echo "exec('cat filename') time: " . $end . "s\n";
?>

我已多次运行此代码以确认显示的结果,并且每次订购时都相同。这里是其中之一的示例:

I have ran this code several times to confirm the results shown and every time I had the same order. Here is an example of one of them:

$ php test.php
file_get_contents() time: 0.0006861686706543s
readfile() time: 0.00085091590881348s
exec('cat filename') time: 0.0048539638519287s

正如您所见 file_get_contents()首先到达 readfile()然后 cat

As you can see file_get_contents() comes first then arrives readfile() and finally cat

至于 cat 即使它是一个 UNIX 命令(这么快,一切都很:) :)我明白调用一个单独的二进制文件可能会导致相对较高的结果。
但是我有点难以理解的是,为什么 file_get_contents() readfile()?毕竟这大约是 1.3倍

As for cat even though it is a UNIX command (so fast and everything :)) I understand that calling a separate binary may cause the relative high result. But the thing I have some difficulty to understand is that why is file_get_contents() faster than readfile()? That's about 1.3 times slower after all.

这两个函数都是内置的,因此非常优化,因为我启用了缓存,readfile( )不是尝试将数据输出到 stdout ,但就像file_get_contents()一样,它会将数据放入RAM中。

Both functions are built-in and therefore pretty well optimized and since I enabled the cache, readfile() is not "trying" to output the data to stdout but just like file_get_contents() it will put the data inside the RAM.

我在这里寻找技术性的低级解释,以了解 file_get_contents()和<$ c的利弊$ c> readfile()除了一个被设计为直接写入stdout而另一个在RAM内部进行内存分配这一事实。

I am looking for a technical low-level explanation here to understand the pros and cons of file_get_contents() and readfile() besides the fact that one is designed to write directly to stdout whereas the other does a memory allocation inside the RAM.

提前致谢。

推荐答案

file_get_contents 仅加载来自文件在内存中,而 readfile cat 也会在屏幕上输出数据,因此它们只执行更多操作。

file_get_contents only loads the data from the file in memory, while both readfile and cat also output the data on the screen, so they just perform more operations.

如果您想将 file_get_contents 与其他人进行比较,请添加 echo 之前

If you want to compare file_get_contents to the others, add echo before it

此外,您没有释放为$ foo分配的内存。如果你将file_get_contents作为最后一次测试移动,你可能会得到不同的结果。

Also, you are not freeing the memory allocated for $foo. There is a chance that if you move the file_get_contents as last test, you will get different result.

此外,你正在使用输出缓冲,这也会造成一些差异 - 只是尝试在输出缓冲代码中添加其余的函数以消除任何差异。

Additionally, you are using output buffering, which also cause some difference - just try to add the rest of the functions in an output buffering code to remove any differences.

在比较不同的函数时,其余代码应该相同,否则你可以接受各种各样的影响。

When comparing different functions, the rest of the code should be the same, otherwise you are open to all kinds of influences.

这篇关于PHP性能file_get_contents()vs readfile()和cat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆