Skills for improving the efficiency of element search and de-duplication in PHP arrays, php Arrays

Source: Internet
Author: User

Skills for improving the efficiency of element search and de-duplication in PHP arrays, php Arrays

Improves the efficiency of searching array elements
1. php in_array method description

Php searches for the existence of array elements. The in_array method is generally used.

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
Array used for comparison

Strict
If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.

Return Value
If the needle is found, TRUE is returned; otherwise, FALSE is returned.


2. in_array element search efficiency

When the compared array haystack is large, the in_array efficiency will be very low.

Example: Use in_array to perform 0.1 million comparisons on arrays with 1000 Elements

<? Php $ arr = array (); // create an array of 0.1 million elements for ($ I = 0; $ I <100000; $ I ++) {$ arr [] = $ I;} // record start time $ starttime = getMicrotime (); // create 1000 random numbers and compare them with in_array ($ j = 0; $ j <1000; $ j ++) {$ str = mt_rand (); in_array ($ str, $ arr );} // record end time $ endtime = getMicrotime (); echo 'run time :'. (float) ($ endtime-$ starttime) * 1000 ). 'Ms <br> ';/*** get microtime * @ return float */function getMicrotime () {list ($ usec, $ sec) = explode ('', m Icrotime (); return (float) $ usec + (float) $ sec;}?>
run time:2003.6449432373ms

Use in_array to determine whether an element exists. It is compared to 0.1 million times in the array of 1000 elements, and the running time takes about 2 seconds.


3. Methods to Improve the efficiency of searching Elements

We can first use array_flip for key-value swaps, and then use the isset method to determine whether an element exists, which improves efficiency.

Example: Use array_flip to perform key-value swaps first, and then use the isset method to determine whether to compare 0.1 million times in the array of 1000 Elements

<? Php $ arr = array (); // create an array of 0.1 million elements for ($ I = 0; $ I <100000; $ I ++) {$ arr [] = $ I;} // key-value interchange $ arr = array_flip ($ arr); // record start time $ starttime = getMicrotime (); // create 1000 random numbers and compare them with isset ($ j = 0; $ j <1000; $ j ++) {$ str = mt_rand ); isset ($ arr [$ str]);} // record end time $ endtime = getMicrotime (); echo 'run time :'. (float) ($ endtime-$ starttime) * 1000 ). 'Ms <br> ';/*** get microtime * @ return float */function getMicrotime () {list ($ use C, $ sec) = explode ('', microtime (); return (float) $ usec + (float) $ sec ;}?>
run time:1.2781620025635ms

Array_flip and isset are used to determine whether an element exists. The number of times in the array of 0.1 million elements is 1000, and the running time is about 1.2 milliseconds.


Therefore, compared with large arrays, The array_flip and isset methods are much more efficient than in_array.


Fast deduplication
1. remove duplicates using the array_unique Method

To deduplicate array elements, we usually use the array_unique method. This method can be used to deduplicate the elements in the array.

<?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);print_r($arr);?>

Output:

Array(  [0] => 1  [1] => 2  [2] => 3  [3] => 4  [4] => 5  [5] => 6  [6] => 7  [7] => 8  [8] => 9)

After deduplication, the key values are not sorted in order. You can use array_values to re-sort the key values.


2. De-duplication efficiency using the array_unique Method

<? Php $ arr = array (); // create an array of 100000 random elements for ($ I = 0; $ I <100000; $ I ++) {$ arr [] = mt_rand ();} // record start time $ starttime = getMicrotime (); // deduplication $ arr = array_unique ($ arr ); // record end time $ endtime = getMicrotime (); $ arr = array_values ($ arr); echo 'unique count :'. count ($ arr ). '<br>'; echo 'run time :'. (float) ($ endtime-$ starttime) * 1000 ). 'Ms <br> '; echo 'use memory :'. getUseMemory ();/*** get memory usage * @ return float */function getUseM Emory () {$ use_memory = round (memory_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

The array_unique method is used to remove duplicates. The running time is about 650 ms, and the memory usage is about 5 m.


3. Faster array deduplication Method

Php has a key-value interchange method, array_flip. We can use this method to remove duplicates. Because of key-value SWAps, the original duplicate values will become the same key.
Then perform another key-value swap. You can replace the key and value to complete deduplication.

<? Php $ arr = array (); // create an array of 100000 random elements for ($ I = 0; $ I <100000; $ I ++) {$ arr [] = mt_rand ();} // record start time $ starttime = getMicrotime (); // use key-value swaps to remove duplicates $ arr = array_flip ($ arr); // record end time $ endtime = getMicrotime (); $ arr = array_values ($ arr); echo 'unique count :'. count ($ arr ). '<br>'; echo 'run time :'. (float) ($ endtime-$ starttime) * 1000 ). 'Ms <br> '; echo 'use memory :'. getUseMemory ();/*** get memory usage * @ ret Urn float */function getUseMemory () {$ use_memory = round (memory_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

The array_flip method is used to remove duplicates. The running time is about 18 ms, and the memory usage is about 2 m.

Therefore, the array_flip method is less than the array_unique method, and the memory usage is reduced by 98%;

Articles you may be interested in:
  • Php searches for the specified value in the array.
  • Php array lookup functions in_array (), array_search (), and array_key_exists () Use instances
  • Php Array Function Sequence: in_array () to find whether the array value exists
  • Array_key_exists () of the php Array Function Sequence-query whether the array key name exists
  • PHP array deduplication Method
  • Php two-dimensional array merging and deduplication
  • Php Bubble sorting, quick sorting, quick search, two-dimensional array deduplication instance sharing
  • Php array deduplication instance and Analysis
  • Php array deduplication Function Code
  • Analysis on de-duplication of two-dimensional arrays in PHP
  • Php array comparison to find the continuous number
  • Php uses the array_search function to implement array search

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.