Robert Cummings wrote:
On Thu, 2006-07-27 at 01:35, Larry Garfield wrote:
On Wednesday 26 July 2006 21:41, Robert Cummings wrote:
I'm working on some code that would be called to generate a cell in a
possibly large table and therefore a small difference in performance
may have a significant impact.
PHP uses copy-on-write and so copies are essentially shared until such
time as you modify one of them. If you don't need references then copies
are faster than references.
By the same token, then, if I have a function that generates a large string
and returns it, is there any benefit to return-by-reference?
Nope. You should only use references if you really need them. Attempting
to improve efficiency by using references instead of copies when you
aren't actually in need of a reference will result in less efficiency
since the engine spends more time creating the reference than the copy.
Cheers,
Rob.
Then how come when I do a foreach on an array (without modifying
anything within the foreach), it still makes a copy of the array that
consumes memory? I think it's dangerous to generalize that it's always
best to let PHP make copies of things. In the foreach situation, the
preferred solution when memory is a problem is to either use a
reference, or have foreach iterate over the keys of the array.
Regards, Adam.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php