Page 1 of 1

This is stone crazy!

Posted: Fri Aug 02, 2002 12:39 pm
by BDKR
Just as a note or perhaps a tip:

Iterating over arrays inside objects is roughly 2 to 3 times slower than iterating over local vars.
So....

Code: Select all

# Note that we are inside a class here
while($something==$something_else)
    { ++$this->array; }
is allways going to be slower than

Code: Select all

while($something==$something_else)
    { ++$array; }
Now the only problem is that if you have objects that need to iterate over an array, how do you get the array into the object or make the object somehow aware of the array without encpasulating that array into the object?

Well, according to something I stumbled across concerning the performance of PHP objects, there is such a thing as "True references".
Hi,
I'm in the process of cleaning up the use of $this-> in my code.
However, as you know, I work with COM, and it is not a good idea to set a local variable to an object, and not freeing up and releasing objects will leak memory. Thankfully, PHP4 has a feature (new to version 4) that most other languages lack... true references.

Instead of your code:
$avar = $this->avar;

Just write:
$avar =& $this->avar;

This makes $avar a true reference, and (I believe) you should be able to work with $avar in loops without performance loss as it does not have to look up the $this.
Great! The are some fantastic implications to this. BUT WHY ISN'T IT IN THE MANUAL! :evil: Yeah, sure, the manual is my friend, but that's as long as it tells me everything (!) I need to know. I've been beating my head over this stuff all morning! Undocumented Hell is NOT a nice place to be.

Anyways, I hope this helps someone. Plus, I'd love to hear feedback on this.
BDKR

Posted: Fri Aug 02, 2002 1:25 pm
by llimllib
I feel your pain - I stumbled across the very same document on object performance. After reading a whole bunch, I've decided to stick with functions for right now because of the performance costs of objects. Basically, references seem to not be faster than copies, and the whole thing is broken. I think I read in some interview with a core developer (zeev or stig, i think) that the PHP object model was created in one night on a whim right before the release of PHP 4. I'll wait for PHP 5 before I start to use objects for anything non-trivial.