Re: array_reverse() and garbage collection

From: Date: Sun, 07 Dec 2008 12:58:50 +0000
Subject: Re: array_reverse() and garbage collection
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-284250@lists.php.net to get a copy of this message
What about using array_pop to take the bottom array element from the first one and add it to the second? So basically you are feeding the 2nd array from the bottom of the 1st one, and assuming garbage collection is done for each call of array_pop, then you'd only need the memory usage of one copy of the array, not two. The downside is that it's not going to be as fast as just looping through, and making two copies, but the upside is less memory usage. I can see it being particularly useful for processing large arrays on memory restricted webservers which would possibly not have the ram for two copies of the array in memory at the same time. Not sure if that actually helps, seeing as array_reverse() is already a function that's probably written in C which could just make a copy with re-arrange the pointers or something. Michael Kubler *G*rey *P*hoenix *P*roductions <http://www.greyphoenix.biz> Richard Heyes wrote:
Hi, So I've been thinking, because occassionally I do, about array_reverse() and how to implement it. Of course, it's entirely theoretical, because it would a total waste of time. But I'm wondering which of two methods would be best. 1. Do it "correctly" and in place. ie, loop through the the first half of the array once swapping element 0 with the end one, element 1 with the next to last one and so on. 2. Loop thru the array once creating a new one. Normally this would take up a little more memory because you end up with two copies of the array in memory just before the end of the function. But what if you set the original element to null once you've assigned it to the new array? The most memory I can see it using is two copies of one element. But only if garbage collection kicks in before you start on the next element. Does garbage collection kick in that often? From what I remember from an article by Derick Rethans, if the refcount is zero, then it gets cleaned up. Cheers.


« previous php.general (#284250) next »