An array of PHP techniques for improving the efficiency of element lookup and element-weight-resolution _php techniques

Source: Internet
Author: User

Improve the efficiency of finding array elements
1.php In_array Method Description

PHP looks for the presence of array elements, typically using the In_array method.

BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])

Parameter description:
Needle
The value to search for, if needle is a string, the comparison is case-sensitive.

Haystack
The array to compare

Strict
If the value of the third argument strict is TRUE, 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 returns FALSE.


2.in_array Find element efficiency

In_array efficiency is low when the array of comparisons is haystack larger

Example: Use In_array to compare 1000 times for arrays with 100,000 elements

<?php
$arr = Array ();

Creates an array of 100,000 elements for
($i =0 $i <100000; $i + +) {
  $arr [] = $i;
}

Record start time
$starttime = Getmicrotime ();

Randomly create 1000 digits using In_array for
($j =0; $j <1000; $j + +) {
  $str = Mt_rand (1,99999);
  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 ("" , Microtime ());
  return (float) $usec + (float) $sec;
>

Run TIME:2003.6449432373MS

Use In_array to determine if an element exists, 1000 times in an array of 100,000 elements, and run time of about 2 seconds


3. Improve the search element efficiency method

We can use Array_flip to Exchange key values, and then use the Isset method to determine whether the element exists, which can improve efficiency.

Example: Use Array_flip to exchange the key value first, then use the Isset method to judge, in 100,000 elements of the array 1000 times

<?php
$arr = Array ();

Creates an array of 100,000 elements for
($i =0 $i <100000; $i + +) {
  $arr [] = $i;
}

Key-value swaps
$arr = Array_flip ($arr);

Record start time
$starttime = Getmicrotime ();

Randomly create 1000 digits using Isset for
($j =0; $j <1000; $j + +) {
  $str = Mt_rand (1,99999);
  Isset ($arr [$str]);
}

Record end time
$endtime = Getmicrotime ();

Echo ' Run time: '. (float) (($endtime-$starttime) *1000). ' Ms<br> ';

/**
 * Get microtime
 * @return float
 /
function Getmicrotime () {
  list ($usec, $sec) = Explode ("" , Microtime ());
  return (float) $usec + (float) $sec;
>

Run time:1.2781620025635ms

Using Array_flip and isset to determine if an element exists, 1000 times in an array of 100,000 elements, running time takes about 1.2 milliseconds


Therefore, the use of Array_flip and Isset methods is much more efficient than in_array for large arrays.


Fast to Heavy
1. Use Array_unique method to carry out heavy

We use the Array_unique method, which is used to weigh the elements in an 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 heavy, the key values will not be in order, you can use Array_values to reorder the key values.


2. Use the Array_unique method to do the heavy efficiency

<?php
$arr = Array ();

Creates an array of 100,000 random elements for
($i =0 $i <100000; $i + +) {
  $arr [] = Mt_rand (1,99);
}

Record start time
$starttime = Getmicrotime ();

Go heavy
$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 use memory
 * @return 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:653.39303016663ms use 
memory:5120kb

Use the Array_unique method to go heavy, run time need about 650ms, memory footprint about 5m


3. Faster array-weight method

PHP has a key-value interchange method Array_flip, we can use this method to heavy, because the key value interchange, the original duplicate value will change to the same key.
Then do a key-value swap, the key and the value of the return can be completed to heavy.

<?php
$arr = Array ();

Creates 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 swaps to
$arr = Array_flip ($arr);
$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 use memory
 * @return 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

Use the Array_flip method to go heavy, run time need about 18ms, memory footprint about 2m

Therefore, using the Array_flip method to reduce the running time by using the Array_unique method is reduced by 98%, and the memory footprint is reduced by 4/5;

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.