PHP fwrite和file_put_contents 性能对比测试

先给结论:PHP程序不会连续打非常多的日志记录。一般来说,PHP程序遇到异常,输出一条文件日志,然后就结束了。那么这种情况,应该如何选择?其实从strace的结果已经很明显了,fwrite和file_put_contents系统调用的结果是一样的,而且file_put_contents调用简单,而且是一次php调用,可以节省函数变量传递。因此,在一般情况下,使用file_put_contents是一个不错的选择。

一、性能测试

先和大部分的文章一样,从性能的方式入手:

<?php
$start_time = microtime(true);
$fp = fopen("fwrite.txt","w");
for ($i = 1;$i <= 1000000;++$i) {
    fwrite($fp, "{$i}\r\n");
}
fclose($fp);
$end_time = microtime(true);
echo "fwrite cost:",($end_time-$start_time),"\r\n";
 
$start_time = microtime(true);
for ($i = 1;$i <= 1000000;++$i) {
file_put_contents("file_put_contents.txt","{$i}\r\n",FILE_APPEND);
}
$end_time = microtime(true);
echo "file_put_contents cost:",($end_time-$start_time),"\r\n";

结果输出:

fwrite cost:46.916671991348
file_put_contents cost:437.18376493454

将fopen和fwrite放入循环:

<?php
$start_time = microtime(true);

for ($i = 1;$i <= 1000000;++$i) {
    $fp = fopen("fwrite.txt","w");
    fwrite($fp, "{$i}\r\n");
    fclose($fp);
}

$end_time = microtime(true);
echo "fwrite cost:",($end_time-$start_time),"\r\n";
 
$start_time = microtime(true);
for ($i = 1;$i <= 1000000;++$i) {
file_put_contents("file_put_contents.txt","{$i}\r\n",FILE_APPEND);
}
$end_time = microtime(true);
echo "file_put_contents cost:",($end_time-$start_time),"\r\n";

结果输出:

fwrite cost:851.75481009483

file_put_contents cost:451.74156188965

可以看出fwrite和file_put_contents的性能差别最要体现在fopen与fclose放的位置。

二、strace分析

我们用strace分析一下,为何fwrite比较快,简单修改一下PHP脚本,在for循环中的$i条件改为小于等于2,即执行2次。

<?php
$start_time = microtime(true);
$fp = fopen("fwrite.txt","w");
for ($i = 1;$i <= 2; ++$i) {
    fwrite($fp, "{$i}\r\n");
}
fclose($fp);
$end_time = microtime(true);
echo "fwrite cost:",($end_time-$start_time),"\r\n";
 
$start_time = microtime(true);
for ($i = 1;$i <= 2; ++$i) {
file_put_contents("file_put_contents.txt","{$i}\r\n",FILE_APPEND);
}
$end_time = microtime(true);
echo "file_put_contents cost:",($end_time-$start_time),"\r\n";

执行:strace php test.php(假设测试文件名是test)

输出:

......

open("/home/lizhibin/php_fwrite_test/fwrite.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
write(3, "1\r\n", 3)                    = 3
write(3, "2\r\n", 3)                    = 3
close(3)                                = 0
write(1, "fwrite cost:", 12fwrite cost:)            = 12
write(1, "0.0008699893951416", 180.0008699893951416)      = 18
write(1, "\r\n", 2
)                     = 2
getcwd("/home/lizhibin/php_fwrite_test", 4096) = 31
lstat("/home/lizhibin/php_fwrite_test/file_put_contents.txt", 0x7fff415c5c40) = -1 ENOENT (No such file or directory)
open("/home/lizhibin/php_fwrite_test/file_put_contents.txt", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
lseek(3, 0, SEEK_CUR)                   = 0
write(3, "1\r\n", 3)                    = 3
close(3)                                = 0
getcwd("/home/lizhibin/php_fwrite_test", 4096) = 31
lstat("/home/lizhibin/php_fwrite_test/file_put_contents.txt", {st_mode=S_IFREG|0664, st_size=3, ...}) = 0
open("/home/lizhibin/php_fwrite_test/file_put_contents.txt", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=3, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
lseek(3, 0, SEEK_CUR)                   = 0
write(3, "2\r\n", 3)                    = 3
close(3)                                = 0

......

注意到标红的语句,使用file_put_contents,都会在写入时先打开文件,在写入结束后在关闭文件。然而使用fwrite,仅仅需要打开一次文件即可,因此,使用fwrite块就不足为奇了。

猜你喜欢

转载自blog.csdn.net/JineD/article/details/111083872