Optimization of PHP array intersection

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. However, because of the many parameters of the mobile phone, and the parameter differences between different mobile phone, so the parameter table structure is usually vertical... "> <LINKhref =" http://www.php100.com//statics/style/headfl

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 product IDs (int), the following data is generated on the premise that:

 

  

$ Rand = function (){

$ Result = array ();

For ($ I = 0; I I <1000; $ I ++ ){

$ Result [] = mt_rand (1, 10000 );

}

Return $ 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:

 

  

$ Time = microtime (true );

$ Result = array_intersect ($ param_a, $ param_ B );

$ Time = microtime (true)-$ time;

Echo "array_intersect: {$ time} \ n ";

?>

Before optimization, let's take a look at some special aspects of array_intersect:

  

$ Param_a = array (1, 2, 2 );

$ Param_ B = array (1, 2, 3 );

Var_dump (

Array_intersect ($ param_a, $ param_ B ),

Array_intersect ($ param_ B, $ param_a)

);

?>

Array_intersect ($ param_a, $ param_ B): 1, 2, 2

Array_intersect ($ param_ B, $ param_a): 1, 2

That is to say, if there are repeated elements in the first array parameter, array_intersect returns all repeated elements that meet the conditions. It is best to be compatible with these functions when rewriting array_intersect.

The following describes the performance achieved through the custom method int_array_intersect:

  

Function int_array_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 ){

Continue;

}

If ($ a [$ I]> $ B [$ j] & ++ $ j ){

Continue;

}

$ Result [] = $ a [$ I];

If (isset ($ a [$ next = $ I + 1]) & $ a [$ next]! = $ A [$ I]) {

+ + $ J;

}

++ $ I;

}

Return $ result;

};

$ Result = array_shift ($ args );

Sort ($ result );

Foreach ($ args as $ arg ){

Sort ($ arg );

$ Result = $ intersect ($ result, $ arg );

}

Return $ result;

}

$ Time = microtime (true );

$ Result = int_array_intersect ($ param_a, $ param_ B );

$ Time = microtime (true)-$ time;

Echo "int_array_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

Int_array_intersect: 0.0026049613952637

Why? The reason is that int_array_intersect operates on integers, while array_intersect operates on strings. if you send an integer to it, it performs a one-step "(string)" type conversion operation.

Note: The test result is based on PHP5.3.5. conclusions of different versions may be different.

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.