Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement fwrite and file_put_contents performance testing in PHP

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to achieve fwrite and file_put_contents performance testing in PHP. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Function microtimeFloat () {

List ($usec,$sec) = explode ("", microtime ())

Return ((float) $usec + (float) $sec)

}

1. Test file_put_contents

The copy code is as follows:

Test results:

There was an error in opening the file during the test, and the data written after the completion of the program execution was incomplete, with only 999997 lines and 3 lines missing. The most important point is that it took more than 307 seconds to use fwrite, while it only took more than 10 seconds to use it.

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

two。 Test fwrite

The copy code is as follows:

Test results:

Write 1 million lines and finish it in about 10 seconds, which is not bad for PHP. This is tested on my personal computer, and it may be faster if it is tested on a production machine.

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

If you want to write a large amount of data to a file, it is recommended to use fwrite instead of file_put_contents. Fwrite is also recommended for highly concurrent requests.

This is the end of this article on "how to achieve fwrite and file_put_contents performance testing in PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report