: This article mainly introduces the query performance of the in_array Large Array. if you are interested in the PHP Tutorial, please refer to it. Problem
Recently, when implementing a project interface, we found that when the array is too large, the data returns slowly. The maximum response time returned by the interface data is 2 s. after repeated debugging, it is found that the most time-consuming part of the code segment isin_array()
Function.
Solution process
In stackoverflow, I found an article that provides my solutions.
-Which is faster, array_key_exists or array_search?
The article says:
array_key_exists
Is much faster.array_search
Must traverse the whole array, so it is O (n ).array_key_exists
Is a hash table lookup, so it is O (1 ).
---- Split line ----
I think it's faster for PHP to check for keys (array_key_exists()
Or simplyisset($array[$key]))
. To search for a value, PHP must cycle through the array; to search for a key PHP will use a hash function.
This array is used to save the user account and is not destroyed and stored in the memory.
Original array
$array=array('account1','account2','$account3');
Modified array
$array=array('account1'=>0,'account2'=>0,'$account3'=>0)
Useisset($array[$account]))
To check whether the account exists in this array.
Summary
Becausein_array()
The function traverses and queries the array. O (n) increases as n (array length) increases. Thereforein_array()
Efficiency should be considered for functions.
When dealing with large array queries, we should try to use key queries instead of value queries in PHP.
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above describes the in_array large array query performance issues, including the content, hope to be helpful to friends interested in PHP tutorials.