I'm doing a thing these days. Study PHP reads more rows of files (probably millions of rows). Consider the problem of efficiency. A simple study was made.
The efficiency of the first. File () function.
The file () function is very efficient if it is a regular document. For example, a corresponding data for each line. So try not to use the file () function
You can use File_get_contents () and then cut with explode. This will be one-third faster.
As an example:
The file styles are as follows:
11111n
22222n
33333n
44444n
55555n
... n
nnnnnnnnnnnn
If you read it in file ($file), it takes a long time.
You can explode ("n", file_get_contents ($file)) in the following ways, and the efficiency will be much faster.
The second one is the way the array is traversed.
The data has been read into the array. The following is the traversal.
All I need is to determine if there is a value in the array, such as whether 44444 is in the array. The first thought is In_array ()
However, the experiment found that the efficiency is very low. Then refer to other people's code to think of a way. Turn the array over. All values are 1. The original value becomes an index. So as long as I write in if ($arr [index]==1) to judge. So much more efficient.
In the traversal of an array. If the array is very large and the data in the array is not used by the Almighty, it is best to extract the array that is used to traverse it. This will improve the efficiency of the process.
Third, the storage of the array.
Save the data after the calculation. There is a file. Three methods have been considered. One is to write directly to PHP files. One is serialize, the other is a JSON string.
The first way
Write files directly to save into PHP
Direct require in when needed.
The second way. Serialize the variable and then file_put_contents () into the file. When used, the unserialize is OK.
The third type is similar to the second. It's just a JSON string.
After testing, the second highest efficiency was found. The third is the second. The first is the slowest. The difference I expected was great.