Live data from Hacker News

How big are PHP arrays (and values) really?

nikic.github.com

21–30 of 90 posts

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

#21
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.

Yes and No. I run a 64-bit Windows 7 machine, but oddly enough the Fedora 14 machine is 32 bit. Yet it yields similar numbers to the 64-bit Windows.

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

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

Python and Ruby both have separate arrays and hashes. They're completely different data structures in both cases.

Python's dict type is a hash table like you'd expect. Python's list is a pointer-array-backed list. (it may inline ints/similar things--I don't remember if CPython does, and exact details are implementation-dependent), and raw arrays are in the standard library if you need them.

From a very quick check, a list of 100k ints in CPython is ~1.5mb, and a dict of 100k ints -> other ints is ~6mb.

Ruby's hash and array implementations are similar, I think, although I don't know Ruby as well, so I don't know the specifics.

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

#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 compiled with head protection :)

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

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

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

#26
post #20
post #8

Earlier quoted context omitted.

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

They are ordered by insertion. That's different from for instance objects in Javascript.

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

#27

Has anyone run similar tests on the soon-to-be-released PHP 5.4? From what I understand, one of the changes in that release is reduced memory consumption. Andi Gutmans has said that PHP 5.4 could lower PHP's memory footprint by as much as 35%.

I actually did most of my tests with PHP 5.4 and trunk binaries, but also tested PHP 5.3 and the numbers didn't change. PHP 5.2 used 8 bytes less, because the circular GC was introduced only in PHP 5.3.

By the way, you can test that yourself too. The codepad I posted the same on has a switch for PHP 5.2, 5.3 and 5.4, so you can easily see for yourself :)

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

#28
post #4
post #2

As the author mentions, if you run into a use-case where you need to store 100000 integers in memory, then you should use one of the many alternative structures available. Some of them were explicitly designed to store integers in an efficient manner. Arrays weren't designed to store integers efficiently or anything else for that matter. They were designed to be fast and easy to use.

I wouldn't call using 56 bytes to store 8 bytes of actual data efficient.

That'd be why he said "arrays weren't designed to store integers efficiently".

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

#29
post #21

Earlier quoted context omitted.

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.

Yes and No. I run a 64-bit Windows 7 machine, but oddly enough the Fedora 14 machine is 32 bit. Yet it yields similar numbers to the 64-bit Windows.

Sounds like you're running a 32-bit interpreter on your 64-bit machine.

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

#30
post #21

Earlier quoted context omitted.

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.

Yes and No. I run a 64-bit Windows 7 machine, but oddly enough the Fedora 14 machine is 32 bit. Yet it yields similar numbers to the 64-bit Windows.

Are you running a 64-bit build of PHP though?
Post reply on HN