PHP fwrite and file_put_contents Performance test code _php skills

Source: Internet
Author: User
Tags sprintf

function Microtimefloat () {
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}

1. Test file_put_contents

Copy Code code as follows:

<?php
$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 written after the execution of the program was incomplete, only 999997 lines, and 3 lines were missing. The most important point is that the time spent more than 307 seconds, and with 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 Code code as follows:

<?php
$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;
    for ($j = 0; $j < $itemCount; $j + +) {
       $itemId = Mt_rand (1, 30 0000);
       $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, 10 seconds or so written, for PHP, the speed is good. This was tested on my PC, 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 number of data to the file, it is recommended to use fwrite, do not use file_put_contents. Fwrite is also recommended in high concurrent requests.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.