PHP中fwrite与file_put_contents性能测试代码
function microtimeFloat() {
list($usec,$sec) = explode(" ", microtime());
return((float)$usec + (float)$sec);
}
1.测试file_put_contents
复制代码 代码如下:
$userCount = 1000;
$itemCount = 1000;
$file = 'ratings.txt';
file_exists($file) &&unlink($file);
$timeStart = microtimeFloat();
for ($i = 0; $i $uid =random(32);
for ($j = 0;$j $itemId = mt_rand(1, 300000);
$rating = $j == 0 ? 1 : mt_rand(1, 100) / 100;
$line = sprintf("%s,%d,%s\n", $uid, $itemId, $rating);
file_put_contents($file, $line, FILE_APPEND);
}
}
$timeEnd = microtimeFloat();
echo sprintf("Spend time: |%s| second(s)\n", $timeEnd -$timeStart);
?>
测试结果:
测试过程中出现了打开文件的错误,而且程序执行完成以后写入的数据不完整,只有999997行,漏了3行。最重要的一点是时间花了307秒多,而用fwrite只花了10秒多的时间,差距还是不小的。
D:\myphp\research>php test2.php
PHP Warning: file_put_contents(ratings.txt): failed to open stream:Permission
denied in D:\myphp\research\test2.php on line 79
Warning: file_put_contents(ratings.txt): failed to open stream:Permission denie
d in D:\myphp\research\test2.php on line 79
Spend time: |307.0586669445|second(s)
...
999994:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,167670,0.15
999995:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,234223,0.13
999996:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,84947,0.79
999997:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,6489,0.38
2.测试fwrite
复制代码 代码如下:
$userCount = 1000;
$itemCount = 1000;
$file = 'ratings.txt';
file_exists($file) &&unlink($file);
$fp = @fopen($file, 'ab');
if (!$fp) die("Open $file failed");
$timeStart = microtimeFloat();
for ($i = 0; $i $uid =random(32);
for ($j = 0;$j $itemId = mt_rand(1, 300000);
$rating = $j == 0 ? 1 : mt_rand(1, 100) / 100;
$line = sprintf("%s,%d,%s\n", $uid, $itemId, $rating);
fwrite($fp, $line);
$k++;
}
}
if ($fp) @fclose($fp);
$timeEnd = microtimeFloat();
echo sprintf("Spend time: |%s| second(s)\n", $timeEnd -$timeStart);
?>
测试结果:
写一百万行记录,10秒左右写完,对于PHP来说,速度算不错了。这是在我的个人电脑上面测试的,如果在生产机上测试,可能速度还要快一些。
D:\myphp\research>php test2.php
Spend time: |10.764221191406|second(s)
用fwrite写入的数据是完整的
999997,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,246982,0.03
999998,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,240160,0.39
999999,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,46296,0.61
1000000,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,26211,0.14
3.总结
如果要往文件里面写入大量的数据,则推荐用fwrite,不要用file_put_contents。在高并发的请求中也建议用fwrite。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool