In_array large array query performance problems

Source: Internet
Author: User
: 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_existsIs much faster.array_searchMust traverse the whole array, so it is O (n ).array_key_existsIs 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.

    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.