PHP has a regular large file read and write

Source: Internet
Author: User
Tags explode json

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.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.