开发者

memory usage of a PHP array when adding a huge numeric key

开发者 https://www.devze.com 2023-02-15 04:11 出处:网络
If I want to use a PHP non-associative array like a dictionary and add a big key, how much memory will PHP al开发者_JAVA技巧locate?

If I want to use a PHP non-associative array like a dictionary and add a big key, how much memory will PHP al开发者_JAVA技巧locate?

$myArray = Array();
$myArray[6000] = "string linked to ID 6000";
$myArray[7891] = "another key-value pair";

Will PHP also allocate memory for the unused keys 0-5999 and 6001-7890?


No, PHP doesn't implement this like a C style array. Php arrays are associative containers, as the php article on arrays states.

An array in PHP is actually an ordered map. A map is a type that associates values to keys.

Since order is preserved, the array will likely be some kind of binary search tree. If you're unfamiliar with binary search trees I suggest picking up a good data structures book to learn more or check out this wikipedia article for a rundown. Your example above would yield a binary search tree with two nodes -- one for data at key 6000, the other for key 7891.


It won't allocate memory for indexes 0-5999.

0

精彩评论

暂无评论...
验证码 换一张
取 消