PHP array_search and in_array function efficiency issues

藏色散人
Release: 2023-04-07 17:28:01
forward
3603 people have browsed it

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...
Copy after login
$needle = 'test for this'; $flipped_haystack = array_flip($haystack); if ( isset($flipped_haystack[$needle]) ) { print "Yes it's there!"; }
Copy after login
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!
Copy after login

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


        
Copy after login

Result:

原始方法:0.0010008811950684 新方法:0.0069980621337891
Copy after login

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 '原始方法:'.($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 '新方法:'.($time4-$time3);
Copy after login

Result:

原始方法:2.9000020027161 新方法:0.008030891418457
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!