Re: Why does count() make copies of arrays?
| From: | Robert Cummings | Date: | Mon, 14 Aug 2006 22:09:01 +0000 |
| Subject: | Re: Why does count() make copies of arrays? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-240657@lists.php.net to get a copy of this message | ||
On Mon, 2006-08-14 at 17:20 -0400, Robert Cummings wrote:
> On Mon, 2006-08-14 at 13:16 -0400, Adam Zey wrote:
> > I was writing a shell script in PHP (4.4.2) that dealt with a rather
> > large array. To figure out what I needed the new memory limit to be, I
> > did a memory_get_usage() at the end of my script, and came up with about
> > 5.5MB. I then set the memory limit to 8MB.
> >
> > When I tried to run it, the script ran out of memory on the line:
> >
> > $numwords = count($words);
> >
> > However, when I switched to simply incrementing $numwords every time I
> > added an element to $words, the memory limit of 8MB was fine.
> >
> > So my question is, if PHP does copy-on-write, why does PHP make a copy
> > of an array when you use count() on it, which should NOT be modifying
> > the array?
>
> For some reason the memory_get_usage() function wouldn't appear in my
> PHP compilation even after using the --enable-memory-limit flag, and
> rather than dig very deep, I whipped up the following script to test
> your issue (under PHP 4.2.2):
That last line should have said 4.4.2 :)
Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com
|
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'