An array of PHP techniques to improve the efficiency of element lookup and Element de-weight analysis, PHP array
Improve efficiency in finding array elements
1.php In_array Method Description
PHP finds that array elements exist, typically using the In_array method.
BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])
Parameter description:
Needle
The value to be searched, if needle is a string, the comparison is case-sensitive.
Haystack
The array used to compare
Strict
If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack
return value
Returns TRUE if needle is found, otherwise FALSE.
2.in_array Find element efficiency
The In_array efficiency is very low when the comparison array is haystack large
Example: Using In_array to compare an array with 100,000 elements 1000 times
<?php$arr = Array ();//Create an array of 100,000 elements for ($i =0; $i <100000; $i + +) { $arr [] = $i;} Record start Time $starttime = Getmicrotime ();//randomly create 1000 numbers using In_array compare for ($j =0; $j <1000; $j + +) { $str = Mt_rand (1,99999 ); In_array ($str, $arr);} Record end Time $endtime = Getmicrotime (); Echo ' run: '. (float) (($endtime-$starttime) *1000). ' Ms
';/** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec;}? >
Run TIME:2003.6449432373MS
Use In_array to determine whether an element exists, compare 1000 times in an array of 100,000 elements, and run time takes about 2 seconds
3. Improve the method of finding element efficiency
We can use Array_flip for key-value swaps, and then use the Isset method to determine whether an element exists, which can improve efficiency.
Example: Using Array_flip first to Exchange key values, and then use the Isset method to determine the 100,000 elements in the array 1000 times
<?php$arr = Array ();//Create an array of 100,000 elements for ($i =0; $i <100000; $i + +) { $arr [] = $i;} Key value Interchange $arr = Array_flip ($arr);//record start time $starttime = Getmicrotime ();//randomly create 1000 numbers using Isset compare for ($j =0; $j <1000; $j + +) { $str = Mt_rand (1,99999); Isset ($arr [$str]);} Record end Time $endtime = Getmicrotime (); Echo ' run: '. (float) (($endtime-$starttime) *1000). ' Ms
';/** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec;}? >
Run time:1.2781620025635ms
Use Array_flip and isset to determine whether an element exists, compare 1000 times in an array of 100,000 elements, and run time takes about 1.2 milliseconds
Therefore, comparing large arrays, using the Array_flip and Isset methods is much more efficient than in_array.
Fast weight-off
1. Use the Array_unique method to remove the weight
To de-weigh the elements of an array, we generally use the Array_unique method, which can be used to weigh the elements in the arrays.
<?php$arr = Array (1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9), $arr = Array_unique ($arr), $arr = Array_values ($arr);p Rint_r ( $arr);? >
Output:
Array ( [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = = 7
[7] + 8 [8] = 9)
After the weight is gone, the key values are not in order, and the key values can be reordered by using Array_values.
2. Using the Array_unique method to remove weight efficiency
<?php$arr = Array ();//Create an array of 100,000 random elements for ($i =0; $i <100000; $i + +) { $arr [] = Mt_rand (1,99);} Record start Time $starttime = Getmicrotime ();//$arr = Array_unique ($arr);//record end time $endtime = Getmicrotime (); $arr = Array_value S ($arr); Echo ' unique count: '. Count ($arr). '
'; Echo ' run Time: '. (float) (($endtime-$starttime) *1000). ' Ms
'; Echo ' use Memory: '. Getusememory ();/** * get using RAM * @return float */function getusememory () { $use _memory = Round (memor Y_get_usage (True)/1024,2). ' KB '; return $use _memory;} /** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec;}? >
Unique count:99 run TIME:653.39303016663MS use memory:5120kb
Use Array_unique method to go heavy, run time takes about 650ms, memory occupies about 5m
3. Faster array de-weight method
PHP has a key-value interchange method Array_flip, we can use this method to go heavy, because the key value interchange, the original duplicate value will become the same key.
Then do a key exchange, the key and the value back can be done to the weight.
<?php$arr = Array ();//Create an array of 100,000 random elements for ($i =0; $i <100000; $i + +) { $arr [] = Mt_rand (1,99);} Record start Time $starttime = Getmicrotime ();//Use key value interchange to $arr = Array_flip ($arr); $arr = Array_flip ($arr);//record end time $endtime = Getmi Crotime (); $arr = Array_values ($arr); Echo ' unique count: '. Count ($arr). '
'; Echo ' run Time: '. (float) (($endtime-$starttime) *1000). ' Ms
'; Echo ' use Memory: '. Getusememory ();/** * get using RAM * @return float */function getusememory () { $use _memory = Round (memor Y_get_usage (True)/1024,2). ' KB '; return $use _memory;} /** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec;}? >
Unique count:99 run time:12.840032577515ms use memory:768kb
Use Array_flip method to go heavy, run time takes about 18ms, memory occupies about 2m
Therefore, using the Array_flip method to use the Array_unique method to reduce the operating time by 98%, memory consumption decreased by 4/5;
Articles you may be interested in:
- How PHP looks up a specified value in an array
- PHP Array Lookup function In_array (), Array_search (), array_key_exists () usages
- PHP Array function sequence In_array () find out if the array value exists
- PHP Array function sequence array_key_exists ()-Find out if the array key name exists
- PHP array de-weight relatively fast implementation mode
- PHP two-dimensional array merging and de-duplication method
- Php bubble Sort, quick sort, quick find, two-dimensional array to re-share instances
- Example and analysis of PHP array de-weight
- PHP array de-weight function code
- Analysis of the de-weight problem of PHP two-dimensional array
- A method for finding consecutive numbers by PHP array comparison
- PHP uses Array_search function to implement array lookup method
http://www.bkjia.com/PHPjc/1106126.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106126.html techarticle PHP array to improve element lookup and element de-weight efficiency of skill parsing, PHP array to improve the efficiency of finding array elements 1.php In_array method description php Find array elements exist, ...