Home  >  Article  >  Backend Development  >  PHP array_search and in_array function efficiency issues

PHP array_search and in_array function efficiency issues

藏色散人
藏色散人forward
2019-10-17 13:41:503671browse

Problem

In an interface, it was found that it was very time-consuming. After investigating the reason, it was found that when array_search searches for the key of an element in an array, the efficiency increases as the array becomes larger, and the time-consuming increases. Especially for large arrays, it is very time-consuming. This problem also exists in the function in_array.

Solution

After using array_flip, use isset instead of in_array function, and use $array[key] instead of array_search. This can solve the time-consuming problem of large arrays

The following are the notes I copied from the php official website. You can observe the difference in efficiency between the two methods

Original website: https://www.php.net/manual/en/function. in-array.php

If you're working with very large 2 dimensional arrays (eg 20,000+ elements) it's much faster to do this...
$needle = 'test for this';
$flipped_haystack = array_flip($haystack);
if ( isset($flipped_haystack[$needle]) )
{
  print "Yes it's there!";
}
I had a script that went from 30+ seconds down to 2 seconds (when hunting through a 50,000 element array 50,000 times).
Remember to only flip it once at the beginning of your code though!

Correction

Someone commented that array_flip is more efficient than in_array and array_search. After doing some experiments, it is true. This is something I didn't consider originally. This solution is only valid if the in_array and array_search functions are used multiple times. Below are the results of my own experiments. Thanks @puppet for pointing out the problem

<?php
$array = array();
for ($i=0; $i<200000; $i++){
    ##随机字符串
    $array[$i] = get_rand().$i;
}
$str = $array[150000];
$time1 = microtime(true);
array_search($str, $array);
$time2 = microtime(true);
echo &#39;原始方法:&#39;.($time2-$time1)."\n";
$time3 =  microtime(true);
$new_array = array_flip($array);
isset($new_array[$str]);
$time4 = microtime(true);
echo &#39;新方法:&#39;.($time4-$time3);

Result:

原始方法:0.0010008811950684
新方法:0.0069980621337891

Loop 5000 times

$array = array();
for ($i=0; $i<200000; $i++){
    ##随机字符串
    $array[$i] = get_rand().$i;
}
$str = $array[199999];
$time1 = microtime(true);
for ($i=0; $i<5000; $i++){
    array_search($str, $array);
}
$time2 = microtime(true);
echo &#39;原始方法:&#39;.($time2-$time1)."\n";
$time3 =  microtime(true);
$new_array = array_flip($array);
for ($i=0; $i<5000; $i++){
    isset($new_array[$str]);
}
$time4 = microtime(true);
echo &#39;新方法:&#39;.($time4-$time3);

Result:

原始方法:2.9000020027161
新方法:0.008030891418457

The above is the detailed content of PHP array_search and in_array function efficiency issues. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete