In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 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 php can read and write regular large files. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The first item is about the efficiency of the file () function.
The file () function is inefficient if it is a regular file. For example, there is a corresponding piece of data per row. Then try not to use the file () function.
You can use file_get_contents () and then cut with explode. The efficiency will be 1/3 faster.
For example:
The file styles are as follows:
11111\ n
22222\ n
33333\ n
44444\ n
55555\ n
.\ n
Nnnnnnnnnnn\ n
If you read it in file ($file). It takes a long time.
You can use the following method explode ("\ n", file_get_contents ($file)); it will be much faster.
Second, the traversal of the array.
The data has been read into the array. Here is the traversal.
What I need is to determine whether there is a value in the array, such as 44444. The first thing that comes to mind is in_array ()
However, after the experiment, it was found that the efficiency was very low. So refer to other people's code and come up with a way. Flip the array over. All values become 1. The original value becomes the index. So as long as I write ($arr [index] = = 1) in if to judge. Sure enough, the efficiency is much higher.
During the traversal of the array. If the array is very large and the data in the array is not omnipotent, it is best to extract the array for traversal. This will improve a lot of efficiency.
Third, the storage of arrays.
Save the calculated data. It exists in a file. Three methods are considered. One is to write it directly into a php file. One is serialize, the other is json string.
The first way
Write directly to the file and save it as PHP
Require directly in when needed.
The second way. Enter the variable serialize and then file_put_contents () into the file. Unserialize is ok when it is used.
The third way is similar to the second. It's just written as a json string.
It has been tested. It is found that the second kind is the most efficient. The third is the second. The efficiency is on a par with the second. The first is the slowest. It's a far cry from what I expected. What a surprise.
This is the end of the article on "how to read and write regular large files 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 out 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.