Let’s take a look at a comparison of the efficiency of PHP’s mobile phone number attribution query based on dichotomy and traditional query. I hope the article will provide good reference value for friends who require high performance.
Out of the impact of the algorithm on the system Out of curiosity, I decided to experimentally study the impact of the algorithm on system efficiency in an actual production environment. The most important thing about the dichotomy is the query and positioning of ordered data. For example, mobile phone numbers are a very appropriate example of ordered data.
If the amount of data is very small, for example, there are only 10 pieces of ordered data, and to query the 9th piece of data, the polling query needs to query 9 times to determine the result, and the number of binary queries is 3 times (matching the 5th, 8th, and 9th data respectively) records) to determine the result. The larger the amount of data, the efficiency brought by the dichotomy is the factorial increase of process 2, which can greatly improve the operating efficiency of the server, increase user waiting time, and save server resources.
Experimental environment: LAMP
Experimental data: Domestic mobile phone number location. The first 7 digits of the mobile phone number represent a number segment, and all the number segments from 1,300,000 to 1,590,000 are generated in ascending order, approximately 300,000 pieces of data.
Traditional query: For any mobile phone number, intercept the first 7 digits, start from the first record in the database, and loop downward to match. If compared, the query results are returned.
The code is as follows
代码如下 |
复制代码 |
flock($fp,LOCK_SH);
$note = fread($fp,filesize('./data.php')); //读取数据
fclose($fp);
$note = explode("n",$note);
array_pop($note);
array_shift($note);
$num = count($note);
$_data = '';
//循环查询开始
for($i=1;$i<$num;$i++){
$row = explode(" ",$note[$i]);
if($m == $row[0]){
$_data = $row;
break;
}
} |
| Copy code
|
flock($fp,LOCK_SH);
$note = fread($fp,filesize('./data.php')); //Read data
fclose($fp);
代码如下 |
复制代码 |
flock($fp,LOCK_SH);
$note = fread($fp,filesize('./data.php')); //读取数据
fclose($fp);
$note = explode("n",$note);
array_pop($note);
array_shift($note);
$num = count($note); //统计数据库总记录数
$_data = '';
$low = 0; //二分法两端点变量
$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;
}
}
|
$note = explode("n",$note);
array_pop($note);
array_shift($note);
$num = count($note);
$_data = ''; |
//Start of loop query
for($i=1;$i<$num;$i++){
$row = explode(" ",$note[$i]);
if($m == $row[0]){
$_data = $row;
break;
}
}
Actual measurement results: The fastest is 0.03512 seconds, the slowest is 0.63043 seconds, and the average query time is about 0.4 seconds.
Dichotomous query: For any mobile phone number, intercept the first 7 digits. First, match the middle 100,000th piece of data in the database. According to the dichotomy principle, if the matching result is greater than the middle value, reselect the second match to the middle value from 100,000 to 200,000 - the 150,000th piece of data. And so on, until the last correct value is found and the result is returned. Then the number of queries each time 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')); //Read data
fclose($fp);
array_pop($note);<🎜>
array_shift($note);<🎜>
$num = count($note); //Statistics on the total number of records in the database<🎜>
$_data = '';<🎜>
$low = 0; //Dichotomy two endpoint variables<🎜>
$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;
}
}
Actual measurement results: Each query is between 0.034-0.035.
Conclusion: This experiment shows that the dichotomy data query efficiency is more than 10 times faster than the traditional efficiency. There are only 300,000 pieces of data in this experiment. When querying ordinary applied data, the larger the amount of data, the better the superiority of the dichotomy method can be shown (theoretically, tens of millions of data can be accurately queried no more than 30 times). Positioning), when dealing with a larger amount of data, the query efficiency may really give people an intuitive response.
http://www.bkjia.com/PHPjc/632887.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632887.htmlTechArticleLet’s take a look at the efficiency comparison between PHP mobile phone number attribution query based on dichotomy and traditional query. I hope the article will be helpful to everyone. Friends who require high performance will provide good reference value. Out of concern...
|