Live data from Hacker News

How big are PHP arrays (and values) really?

nikic.github.com

31–40 of 90 posts

Re: How big are PHP arrays (and values) really?

#31
post #24
post #12

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…

Ah. Yes. I think you're right. I think PHP is still complied for 32-bit on Windows. That makes more sense.

Re: How big are PHP arrays (and values) really?

#32
post #3

PHP 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';

N.B. when he says at the same, it's really at the same time.

   $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?

#33
post #11

Interesting. 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…

As I see it, doing the ugly special casing for you is exactly what any competent dynamic language implementation should do.

Re: How big are PHP arrays (and values) really?

#34
post #6

I'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";

I don't think someone picking up your code will be pleased with all variables named $a :)

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?

#35
post #25

php 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.

> AFAIC Python and Ruby are also memory hogs.

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?

#36
post #3

PHP 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 think its important to note your first example actually isn't valid. Part of PHP's type-mixing means strings that consist of an integer representation as array keys are automatically casted to int.

  $arr = array(1 => 10, "1" => 11);
  echo $arr[1]; // >> 11

Re: How big are PHP arrays (and values) really?

#37

Interesting. Wonder how this compares to python and ruby memory handling.

I tried with a "pure" Array - for Ruby it's 1Mb for 100 000 elements EDIT: and for Hash with h[i] = i, it's ~6Mb

Does that account for the size taken by the integers themselves?

Re: How big are PHP arrays (and values) really?

#38
> 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 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?

#39
post #3

PHP 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';

It is the same for Javascript: even though you have an Array object that you can instantiate instead of the more generic Object, they are much the same thing under the hood with both arrays and objects being hash maps [you can use the two interchangably to an extent (if you don't need the extra functinos defined by the array prototype) - references object properies in an hash-like manner or array contents the same way as object properties].

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…

I don't see how unions could save space, care to explain?
Post reply on HN