Upfront I want to thank Johannes and Tyrael for their help in finding some of the more hidden memory usage.
In this post I want to investigate the memory usage of PHP arrays (and values in general) using the following script as an example, which creates 100000 unique integer array elements and measures the resulting memory usage:
<?php
$startMemory = memory_get_usage();
$array = range(1, 100000);
echo memory_get_usage() - $startMemory, ' bytes';
How much would you expect it to be? Simple, one integer is 8 bytes (on a 64 bit unix machine and using the long type) and you got 100000 integers, so you obviously will need 800000 bytes. That’s something like 0.76 MBs.
Now try and run the above code. You can do it online if you want. This gives me 14649024 bytes.
Yes, you heard right, that’s 13.97 MB - eightteen times more than we estimated.
So, where does that extra factor of 18 come from?
une version plus optimisée des arrays est proposée par : http://php.net/manual/fr/class.splfixedarray.php
Merci :)
Merci pour les benchs - a priori en écriture il est un peu plus performant aussi.
Je viens de tomber sur cet article qui parle des manipulations array_* et des différents avantages des classes SPL :
http://fr.slideshare.net/patrick.allaert/maitriser-les-structures-de-donnes-php-102-forum-paris-2012
La super bonne nouvelle c'est que dans PHP 5.4 une classe sera moins lourde qu'un array ...
Je viens de tomber sur cette extension :
http://www.php.net/manual/fr/book.judy.php
Un bench tend à montrer qu'il est encore moins gourmand en mémoire :
http://rusty.ozlabs.org/?p=153