Did anyone else even try running his suggested code? I think maybe he has a problem with his setup. I don't get those numbers. My numbers for PHP 5.3.8: Windows 7 - 8524568 bytes (using range) 3600584 bytes (using SplFixedArray) Fedora 14 - 7724600 bytes (using range) 3200568 bytes (using SplFixedArray) *edit - Added numbers for SplFixedArray
Yep, I already got some comments on that. You are either using a 32 bit system or a 32 bit binary (at least I think that the binaries PHP distributes for Windows are compiled for 32 bit, so even if you are on a 64 bit Windows you'll still get 32 bit numbers). The Windows number still is 8 bytes per element larger than the number I wrote (76 per element). This might have various reasons, one could be that it was compi…
How big are PHP arrays (and values) really?
31–40 of 90 posts
Re: How big are PHP arrays (and values) really?
#32PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';
$x = array();
$x['asfd']=123;
$x[] = 4;
var_dump($x);
array(2) {
["asfd"]=>
int(123)
[0]=>
int(4)
}Re: How big are PHP arrays (and values) really?
#33Interesting. Wonder how this compares to python and ruby memory handling.
Since PHP arrays are within a factor of two for the theoretical optimum for a dynamically typed language that unifies arrays and hashes, all these languages (add javascript to the list) should have roughly comparable behaviour unless they do some ugly special casing for hashes where all keys are integers. [edit: see the comment by InfernalH for a measurement that indicates that ruby seems to do this.] You have to loo…
Re: How big are PHP arrays (and values) really?
#34I'm kinda hijacking this thread because I always wonder how to handle vars in PHP. Should we use short named vars like $a, $b? Should we avoid always using the same var and changing its type? $a = 30; $a = "thing";
Use "long" names and use unset() if worried about memory consumption. And more on topic, you can use unset on a array item !
Re: How big are PHP arrays (and values) really?
#35php is not unique in having big memory footprint. AFAIC Python and Ruby are also memory hogs. An interesting question is how memory efficient a dynamic PL can be. Given that in modern computers memory access (cache misses) is fairly expensive it probably makes sense to trade instructions for memory.
Even more so in some areas, for instance a Python `int` is not a machine integer but a full-blown object.
Re: How big are PHP arrays (and values) really?
#36PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';
$arr = array(1 => 10, "1" => 11);
echo $arr[1]; // >> 11Re: How big are PHP arrays (and values) really?
#37Re: How big are PHP arrays (and values) really?
#38This is not valid C usage of unions. They are _only_ for use as a method to save space, not for conversion between types, despite it being a very common usage of unions.
This can cause all manner of problems when compiler optimizations such as type-based alias analysis are used.
EDIT: Turns out I'm completely wrong on this and it's fine from C99 onwards.
Re: How big are PHP arrays (and values) really?
#39PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';
I suspect a number of languages have simiar memory (in)efficiency with their array types because the arrays are implemented this way (and the stuff stored in each slot is untyped so you'll not just store that integer, at very least the engine will need a marker that identifies it as an integer rather than something else).
The high memory use is one of the prices you pay for the type flexibility.
Re: How big are PHP arrays (and values) really?
#40> A union is a means to make some value accessible as various types. For example if you do a zvalue_value->lval you’ll get the value interpreted as an integer. If you use zvalue_value->ht on the other hand the value will be interpreted as a pointer to a hashtable (aka array). This is not valid C usage of unions. They are _only_ for use as a method to save space, not for conversion between types, despite it being a ve…