function Microtimefloat () {
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}
1. Test file_put_contents
Copy the Code code as follows:
$userCount = 1000;
$itemCount = 1000;
$file = ' ratings.txt ';
File_exists ($file) &&unlink ($file);
$timeStart = Microtimefloat ();
for ($i = 0; $i < $userCount; $i + +) {
$uid =random (32);
for ($j = 0; $j < $itemCount; $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);
?>
Test results:
There was an error opening the file during the test, and the data that was written after the execution of the program was incomplete, only 999997 lines and 3 rows were missed. The most important point is that the time spent more than 307 seconds, and the use of fwrite only spent 10 seconds more time, the gap is not small.
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. Test fwrite
Copy the Code code as follows:
$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 < $userCount; $i + +) {
$uid =random (32);
for ($j = 0; $j < $itemCount; $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);
?>
Test results:
Write 1 million lines of records, about 10 seconds to write, for PHP, the speed is good. This was tested on my personal computer, and if tested on a production machine, it might be faster.
d:\myphp\research>php test2.php
Spend time: |10.764221191406|second (s)
The data written with Fwrite is complete.
999997,qovczyfjflfhjigygxac615koxdx3yii,246982,0.03
999998,qovczyfjflfhjigygxac615koxdx3yii,240160,0.39
999999,qovczyfjflfhjigygxac615koxdx3yii,46296,0.61
1000000,qovczyfjflfhjigygxac615koxdx3yii,26211,0.14
3. Summary
If you want to write a large amount of data into the file, it is recommended to use fwrite, do not use file_put_contents. It is also recommended to use fwrite in high-concurrency requests.