The test here is to write 1 million rows of records to the file. if the data volume is small, such as about 1000 records, the gap may be small. function microtimeFloat (){
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}
1. test file_put_contents
The code is as follows:
$ User count = 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:
During the test, an error occurred while opening the file. after the program is executed, the data written is incomplete. there are only 999997 rows, and three rows are missing. The most important thing is that it took more than 307 seconds, while fwrite took more than 10 seconds. 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
The code is as follows:
$ User count = 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 and write them in about 10 seconds. for PHP, the speed is quite good. This was tested on my PC. if it was tested on a production machine, it could 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, we recommend that you use fwrite instead of file_put_contents. Fwrite is also recommended for highly concurrent requests.