简体   繁体   中英

PHP Array Speed: Is 10,000 elements TOO MANY for accessing associative arrays in a timely manner?

Say you have an associative array of about 10,000 elements in size. Is this generally viewed as too BIG of an array and thus undesirable from the standpoint of fast webpage serving? What I want to do is this (And I want to do it fast without a noticable delay):

//assume $theArray has 10,000 elements
echo $theArray["product_4563"];

No, the "arrays" in PHP are actually Maps, or dictionaries.

The key is mapped to the value with a Hash table, so it's an O(1) access anyway.

Most languages support arrays in a way that the insert, remove, access or in O(1) like in a regular array.
Or else it wouldn't really be an array, and thus not named "Array".

Speed won't be your problem for the most part as PHP array element access is very fast. It's memory size you should be concerned about (especially if the individual elements contain a lot of data).

Loading in 10,000 elements to just echo one of them is not as efficient as it could be. This data should be stored in a database and you can just select the desired record, then echo it.

Speed will only come into play if you have many requests per second loading these large arrays or if the source of the data used to fill the array is dependent on external resources. With many simultaneous requests, your memory will approach exhaustion and your web server will start to crawl as it begins employing virtual memory.

That's unpredictable to determine how long an operation will "stall the browser". The time taken to for the browser to render the page depends on much more than the time taken to execute a script. Regarding the speed of the actual operation it shouldn't take more than a millisecond or two. Check here for interesting benchmarks , you'll notice all the results are in microseconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM