Re: Return Values Copied? Copied if By Reference?
| From: | Robert Cummings | Date: | Thu, 27 Jul 2006 02:41:22 +0000 |
| Subject: | Re: Return Values Copied? Copied if By Reference? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239836@lists.php.net to get a copy of this message | ||
On Wed, 2006-07-26 at 22:29, Michael B Allen wrote:
> Is a function return value copied? If the value is an integer I suppose
> it is but what about a string or an array? If you pass by reference is
> the return value still copied?
>
> For example, is this:
>
> function foo(&$arr) {
> $arr[] = "bar";
> }
>
> faster than this?
>
> function foo(&$arr) {
> $arr[] = "bar";
> return $arr; // is this copied?
> }
>
> 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.
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. |
`------------------------------------------------------------'