How PHP improves file query efficiency

Source: Internet
Author: User
How can I improve the query efficiency to find the row starting with a unique string from a file. My current method is very stupid: store the file content in an array by row and use regular expressions to match every element in the array until it is found. This is very inefficient because there are 0.17 million lines in the file, so the most... how can I improve the query efficiency to find the row starting with a unique string from a file.
My current method is stupid:

  • Store the file content into an array by row
  • Use regular expressions to match every element in the array until it is found.

In this way, the efficiency is very low. Because there are 0.17 million rows of files, the minimum cycle is 0.17 million times...
Thank you for your help.

Reply content:

How can I improve the query efficiency to find the row starting with a unique string from a file.
My current method is stupid:

  • Store the file content into an array by row
  • Use regular expressions to match every element in the array until it is found.

In this way, the efficiency is very low. Because there are 0.17 million rows of files, the minimum cycle is 0.17 million times...
Thank you for your help.

Process this file into a structure stored in the dictionary tree (trie) or tree B, and then you can quickly query it.

The above can be too abstract. Let's give you an easy-to-implement algorithm. Although the efficiency is slightly lower than trie/B-tree, it is also enough.

Preprocessing

1. traverse this file and record the offset records of each line as an array of int.
2. Indirectly sort the array. Note that indirect means comparing the rows pointed to by this array element during sorting.
3. Save the Array (less than kb for int values ).

Query

1. Read this array.
2. Use indirect binary search. Note: When searching, compare the corresponding rowsFirst n characters, N = strlen ().

If you do not understand this algorithm, wash and sleep.

It starts with a unique string, meaning that the first is a and the second is not?

  $line){$line = trim($line);$len = strlen($line);if ($len > 0 and $line[0] == "a" and ($len == 1 or $line[1] != "a") ) {echo $line . "\n";}}?>

There is no need to use regular expressions. I don't know if regular expressions in PHP will be compile in advance and how efficient it is.

Amount .... Match the matching rows directly. The entire file is read as a string at a time...
Then, use the regular expression to match the row whose first letter is .. Since you have all said yes, there must be line breaks...

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.