This is stone crazy!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

This is stone crazy!

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply