PHP array intersection optimization code analysis

Source: Internet
Author: User
Suppose we are running a mobile phone-related website. You can specify several parameters (such as operating system, screen resolution, camera pixel, and so on) to filter your desired mobile phone.

Suppose we are running a mobile phone-related website. You can specify several parameters (such as operating system, screen resolution, camera pixel, and so on) to filter your desired mobile phone.

However, because mobile phones have many parameters and their parameters vary greatly, the parameter table structure is generally a vertical table (one parameter is a row ), instead of a horizontal table (a parameter is a column), several parameters are used to obtain the result. Generally, each individual parameter is used to obtain the result and the intersection is obtained together.
Assuming that each parameter contains about one thousand unique results (id int), the following data is generated on the premise that:
The Code is as follows:
$ Rand = function (){
$ Result = array ();
For ($ I = 0; I I <1000; null ){
$ Value = mt_rand (1, 10000 );
If (! Isset ($ result [$ value]) {
$ Result [$ value] = null;
$ I ++;
}
}
Return array_keys ($ result );
};
$ Param_a = $ rand ();
$ Param_ B = $ rand ();
?>

Note: If the test dataset is too small, the conclusions may be inconsistent. First, let's look at the performance achieved through the PHP built-in method array_intersect:
The Code is as follows:
$ Time = microtime (true );
$ Result = array_intersect ($ param_a, $ param_ B );
$ Time = microtime (true)-$ time;
Echo "array_intersect: {$ time} \ n ";
?>

Let's take a look at the performance achieved through the custom method intersect:
The Code is as follows:
Function intersect (){
If (func_num_args () <2 ){
Trigger_error ('Param error', E_USER_ERROR );
}
$ Args = func_get_args ();
Foreach ($ args AS $ arg ){
If (! Is_array ($ arg )){
Trigger_error ('Param error', E_USER_ERROR );
}
}
$ Intersect = function ($ a, $ B ){
$ Result = array ();
$ Length_a = count ($ );
$ Length_ B = count ($ B );
For ($ I = 0, $ j = 0; $ I <$ length_a & $ j <$ length_ B; null ){
If ($ a [$ I] <$ B [$ j]) {
$ I ++;
} Else if ($ a [$ I]> $ B [$ j]) {
$ J ++;
} Else {
$ Result [] = $ a [$ I];
$ I ++;
$ J ++;
}
}
Return $ result;
};
$ Result = array_shift ($ args );
Sort ($ result );
Foreach ($ args as $ arg ){
Sort ($ arg );
$ Result = $ intersect ($ result, $ arg );
}
Return $ result;
}
$ Time = microtime (true );
$ Result = intersect ($ param_a, $ param_ B );
$ Time = microtime (true)-$ time;
Echo "intersect: {$ time} \ n ";
?>

Intuitively, we will certainly think that built-in functions are faster than custom functions, but the results in this example are the opposite:
Array_intersect: 0.023918151855469
Intersect: 0.0026049613952637
Please note that the functions of array_intersect and intersect are not fully equivalent. The example is as follows:
The Code is as follows:
$ Param_a = array (1, 2, 2 );
$ Param_ B = array (1, 2, 3 );
Var_dump (
Array_intersect ($ param_a, $ param_ B ),
Intersect ($ param_a, $ param_ B)
);

Array_intersect: 1, 2, 2
Intersect: 1, 2
That is to say, if there are repeated elements in the first array parameter, array_intersect will return all repeated elements that meet the conditions, instead of simply returning one, if you are interested, you can change the Parameter order and then view the result.
When I first wrote the intersect method, I wrote it like the following:
The Code is as follows:
Function intersect (){
If (func_num_args () <2 ){
Trigger_error ('Param error', E_USER_ERROR );
}
$ Args = func_get_args ();
Foreach ($ args AS $ arg ){
If (! Is_array ($ arg )){
Trigger_error ('Param error', E_USER_ERROR );
}
}
$ Result = array ();
$ Data = array_count_values (
Call_user_func_array ('array _ merge', $ args)
);
Foreach ($ data AS $ value => $ count ){
If ($ count> 1 ){
$ Result [] = $ value;
}
}
Return $ result;
}
?>

The code is more concise, but there is a drawback. Because array_merge is used, when there are many elements in the array, the memory occupied will be relatively large. Otherwise, if there are not many elements in the array, this method is also feasible. (Source: huoding notes)

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.