If I have an array which is using numerical keys and I add a key far outside the range so far, does it create the intermediate keys as well. For example if I have
$array = array(1,2,3,4);
$array[9] = 10;
Will this cause php to internally reserve memory for the keys 4-8 even though they don't have a value with them.
The reason I ask is I have a large 2D array which I want to use for memoization in a dynamic programming algorithm because only a small number of the total cells will need to be computed. However it would be taxing on the memory to have an empty array of that size for the full 2D array. Is there a better way to do 2 key memoization? I could use an associative array and append the keys with a separator or some scheme like that but if php won't make the extra keys I would rather (for simplicity and readability) just use the 2D array. Thoughts?
Ctrl+Shift+Esc
, select Performance tab and look at the Memory area (bottom). In PHP create an empty array and then set a big key, such as$arr[PHP_INT_MAX]=null;
. In order to actually see it you may have to halt the script for a while, e.g.sleep(5);
. – Sverri M. Olsen 22 hours ago