Live data from Hacker News

How big are PHP arrays (and values) really?

nikic.github.com

11–20 of 90 posts

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

#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 look that up in the relevant documentatio and/or source code. Perl on the other hand should fare better, as it distinguishes arrays and hahes. If you want performance, use a real programming language.

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

#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

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

#13
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";

Someone downmodded you for being offtopic, but I'll answer you anyway.

The name of the var doesn't matter at all. And you can change the type of a var at will. There is nothing at all in PHP that will be better if you avoid changing the type, so just do what is clearest for your program.

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

#14
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…

Javascript also distinguishes arrays and hashmaps.

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

#15
post #9
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's funny. I don't have much experience with php, and I had to actually run your last example to know that it appended the values (instead of recreating the array with a single value each time). Is there an append operator, or something more explicit ? '+=' seems to do something strange..

[] IS the append operator. And += only works right if both sides are arrays. If not the right side will be converted. += is really intended for hashmaps, not integer indexes, for those use [].

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

#16
post #9
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's funny. I don't have much experience with php, and I had to actually run your last example to know that it appended the values (instead of recreating the array with a single value each time). Is there an append operator, or something more explicit ? '+=' seems to do something strange..

If you want something more explicit you probably want array_push($array,$val1,$val2,$val....);

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

#17
post #9
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's funny. I don't have much experience with php, and I had to actually run your last example to know that it appended the values (instead of recreating the array with a single value each time). Is there an append operator, or something more explicit ? '+=' seems to do something strange..

Not sure exactly what you were trying. Using the [] syntax is common for appending elements (using the next available key). Otherwise you can use the array functions (array_push/pop/etc). As for plain operators, here is how PHP handles array operators: http://www.php.net/manual/en/language.operators.array.php

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

#18
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…

JavaScript engines have many types of arrays. They do indeed resort to "ugly special casing" for objects in which all keys are integers: for instance, SpiderMonkey calls them "dense arrays". V8 has a similar optimization. Likewise, Lua stores a hashtable part and an array part for all objects.

V8 and SpiderMonkey have optimizations for the values, too. When a value is a 31-bit integer (in the case of V8) or any number (in the case of SpiderMonkey), the value itself is optimized to avoid heap allocation. In V8's case optimized values take 32 bits, while in SpiderMonkey's case optimized values take 64 bits. So an array consisting of 100,000 integers will take 400K plus a negligible amount of malloc/GC slop in V8 and 800K in SpiderMonkey.

JavaScript also has typed arrays, which allow the programmer to use fine-grained, optimized array buffers. Performing this experiment with a typed array will yield a 400K buffer in all engines that support the feature.

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

#19
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

Are you on 32-bit, because all his numbers are for 64-bit, except the large table near the beginning, which matches roughly with the numbers you give.

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

#20
post #8
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';

A PHP array is in fact just an ordered hash-map (IE.. hash map with an associated linked list which tracks the iteration order). When you do an array append, the logic PHP runs is it tries to guess what the most logical key would be, add that to the hash-map and then to the end of the linked list. $a = array(); $a[] = 'A'; $a[] = 'B'; print_r($a); Array ( [0] => A [1] => B ) $b = array(); $b[5] = 'A'; $b[100] = 'B';…

I think they are a little more complex than regular hash maps.

1) They keys can be integers or unicode strings (ordering works differently for these two cases)

2) The keys are not necessarily ordered (see ksort and krsort functions, for example).

Post reply on HN