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...