A comparison between the mobile phone number attribution query and the traditional query efficiency based on the two-division method in PHP

Source: Internet
Author: User
Tags comparison explode flock fread

Out of curiosity about the effect of the algorithm on the system, it is decided experimentally to study the effect of the algorithm on the system efficiency in the actual production environment. The most important part of the binary method is to locate the ordered data, for example, the mobile phone number is a very appropriate and orderly data example.
If the amount of data is very small, for example, only 10 ordered data, to query the 9th data, the polling query needs to query 9 times to determine the results, the binary query number of 3 times (respectively, matching the 5th, 8, 9 Records) to determine the results. The greater the amount of data, the efficiency of the binary method is 2 of the factorial increment, can greatly improve the server's operational efficiency, improve user waiting time, save server resources.
Experimental Environment: LAMP
Experimental data: Domestic mobile phone number attribution. The first 7 digits of the cell phone number represent a number segment, generating all segments from 1300000 to 1590000, arranged from small to large, about 300,000 pieces of data.
Traditional query: For any mobile phone number, intercept the top 7 digits, from the first record in the database to start the loop down to match, if the comparison, return the query results.

The code is as follows Copy Code
Flock ($FP, lock_sh);
$note = Fread ($fp, FileSize ('./data.php ')); Reading data
Fclose ($FP);
$note = Explode ("n", $note);
Array_pop ($note);
Array_shift ($note);
$num = count ($note);
$_data = ';
Loop query Start
for ($i =1; $i < $num; $i + +) {
$row = Explode ("", $note [$i]);
if ($m = = $row [0]) {
$_data = $row;
Break
}
}

Measured results: The fastest 0.03512 seconds, the slowest 0.63043 seconds, the average query spents about 0.4 seconds.

Two-way query: For any mobile phone number, intercept the first 7 digits. First match the 100,000th data in the database, according to the principle of duality, if the matching result is larger than the middle value, select the second match 100,000th to No. 200000 of the middle value----150,000th data. And so on, until the query to the last correct value returns the result. The number of queries per query is less than or equal to 17 times.

The code is as follows Copy Code

Flock ($FP, lock_sh);
$note = Fread ($fp, FileSize ('./data.php ')); Reading data
Fclose ($FP);
$note = Explode ("n", $note);
Array_pop ($note);
Array_shift ($note);
$num = count ($note); Total number of statistical database records
$_data = ';
$low = 0; Two-point variables at both ends of the method
$hight = $num;
while ($m < 1599999) {
$num = Ceil (($hight + $low)/2);
$row = Explode ("", $note [$num]);
if ($m = = $row [0]) {
return $_data = $row;
Break
}else{
$m >= $row [0]? $low = $num: $hight = $num;
}
}

Measured results: Each query is between 0.034-0.035.

Conclusion: This experiment shows that the efficiency of binary data query is more than 10 times times faster than traditional efficiency. This experimental data only 300,000, in the general application of data query, the greater the amount of data, the more can show the superiority of the two-point method (in theory, tens of millions of data queries not more than 30 times can be accurate positioning), in a larger number of data, query efficiency may really give people an intuitive response.

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.