Php notes: Analysis of reading and writing regular large files _ PHP Tutorial

Source: Internet
Author: User
Php notes: regular analysis of reading and writing large files. I am doing something these days. research on PHP reading files with a large number of lines (about millions of lines ). consider efficiency issues. A simple study was conducted. summary. the efficiency of the file () function is being done over the past few days. research on PHP reading files with a large number of lines (about millions of lines ). consider efficiency issues. A simple study was conducted. summary:

The efficiency of the first. file () function.

The efficiency of the file () function is very low. if there is a regular file, for example, each row contains a corresponding data, try not to use the file () function.

You can use file_get_contents () and then use explode to cut. this will increase the efficiency by 1/3.

For example:

The file style is as follows:

11111 \ n

22222 \ n

33333 \ n

44444 \ n

55555 \ n

... \ N

Nnnnnnnnnnnnn \ n

If you use file ($ file) to read data, it takes a long time.

You can use the following method to explode ("\ n", file_get_contents ($ file); the efficiency will be much faster.

Second, the array traversal method.

The data has been read into the array. The following is the traversal.

All I need is to make sure that there is no value in the array, for example, whether 44444 is in the array. The first thing that comes to mind is in_array ()

However, the experiment shows that the efficiency is very low. so I thought of a method based on other people's code. flip the array. all values are 1. the original value is changed to an index. as long as I write ($ arr [index] = 1) in if to judge. the efficiency is much higher.

In the process of array traversal, if the array is very large and the data in the array is not fully used, it is best to extract the array to be traversed. this will improve a lot of efficiency.

Article 3: storage of arrays.

Save the computed data. there is a file. three methods are considered. one is to directly write the php file, the other is serialize, and the other is a json string.

Method 1

Write files directly Save as PHP

Request in directly when necessary.

Method 2. Put the variable serialize and then file_put_contents () into the file. when used, unserialize will be OK.

The third method is similar to the second method. it is only written as a json string.

After testing, I found that the second type of efficiency is the highest. The third type is the same as the second type of efficiency. The first type is the slowest. it is a big gap with what I expected. it is really a big drop.

Attention (about millions of rows). Taking efficiency into account, a simple study is conducted. The following is a summary of the efficiency of the. file () function...

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.