On Wed, 2004-03-10 at 08:30, [EMAIL PROTECTED] wrote: > On 10 Mar 2004 Robert Cummings wrote: > > > Overhead is minimal since PHP doesn't actually copy the contents of the > > container until an attempt to modify it is made. At which time the > > contents are only actually copied if the internal reference count is > > greater than 0. Generally this means it won't be copied since your > > returning function will no longer be referencing it. This is not to be > > confused with references as programmers work with them in scripts > > themselves. > > Rob I have a related question, if you know ... what is the point at > which passing objects by reference instead of by value provides a > performance benefit?
Passing by reference appears to provide a small amount of speed increase though IMHO very small. Check the following three scripts: <? $initial = array(); for( $i = 0; $i < 100000; $i++ ) { $initial[$i] = $i; } for( $i = 0; $i < 10000000; $i++ ) { $foo = $initial; } ---------------------------------------------------------------- linux "time" command: 7.46user 0.02system 0:07.58elapsed 98%CPU ---------------------------------------------------------------- <? for( $i = 0; $i < 100000; $i++ ) { $initial = $i; } for( $i = 0; $i < 10000000; $i++ ) { $foo = $initial; } ---------------------------------------------------------------- linux "time" command: 7.25user 0.00system 0:07.25elapsed 99%CPU ---------------------------------------------------------------- <? $initial = array(); for( $i = 0; $i < 100000; $i++ ) { $initial[$i] = $i; } for( $i = 0; $i < 10000000; $i++ ) { $foo = &$initial; } ---------------------------------------------------------------- linux "time" command: 6.85user 0.02system 0:06.87elapsed 99%CPU ---------------------------------------------------------------- As you can see copying by value regardless of the value is pretty much the same (I think the time discrepency is due to a slightly longer time to populate the $initial array than to assign an integer value 100000 times to $initial (which makes sense O( lg n ) versus O( 1 )). It seems passing by reference provides a small amount of advantage (about 5% as shown above). Even though references may be faster I would say now that it depends on what you want to do and who is maintaining the code :) Also copies are definitely a good choice when you are passing data that shouldn't affect some centralized storage variable (such is usually the case for Singleton factories). Also don't forget that the above example is assigning the data 10 million times, generally the difference would be almost negligible. > It sounds from the above like if you are not modifying the object in > the called code then passing by value is always best because this is > treated as a pass by reference unless and until there is a > modification, so there is no performance cost. (I udnerstand that if > modifying it then it must be passed by reference.) Passing references everywhere when a reference isn't needed can make your code more difficult to read since others reading it will constantly be asking, "is this being passed as a reference for a reason? Does changing the value outside of the function returning it or receiving it have some transient effect?". > > If that's right, then ... > > - does the same apply to arrays? Yes as shown above. > - what happens if you pass an object by value and then call one of > its methods which does not modify any data? Does the object get > copied? I checked with the internals list for this answer since I thought it would work the same, and indeed it turns out that objects in PHP 4 do work this way. However this is not quite true with PHP 5 which uses the concept of object-handles. Nonetheless, Derick Rethans informed had the following to say: "in php 5 it is true for "object-handles" and not objects, but that shouldn't be of any concern." 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. | `------------------------------------------------------------' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php