You are here: Re: large string... « PHP Programming Language « IT news, forums, messages
Re: large string...

Posted by Andy Hassall on 10/03/29 11:51

On Wed, 28 Jun 2006 18:00:49 +0200, "B.r.K.o.N.j.A" <thebrkonja@inet.hr> wrote:

>example code
>
>function myfunc1()
>{
> return myfunc2();
>}
>
>function myfunc2()
>{
> return myfunc3();
>}
>
>function myfunc3()
>{
> return very_large_string; // let's say that very large string is 30kb
>of data
>}
>
>
>print myfunc1();
>
>The question is, would these 30Kb of data be copied into each function
>wasting resources or only reference to these data would be propagated to
>myfunc1? PHP version is 5.0.x

PHP uses copy-on-write for the contents of variables. So, there'd only be one
copy of the large string in memory in the above.

Whilst "=" assigns by copy, it's my understanding that it initially just sets
up references. But when it comes to writing to the new variable, it only then
does the copy, giving the same semantics as copy-by-value but giving savings
where the copies are actually just read-only.

$a = 'long string'; // one instance of long string
$b = $a; // still one instance - $b and $a refer to same data
$b .= 'some more'; // only at this point is the data copied, so that $b
// can be modified

More information here: http://www.zend.com/zend/art/ref-count.php

In the case of functions, there's a further hint in the manual that multiple
copies may be avoided automatically:

http://www.php.net/manual/en/language.references.return.php
"Do not use return-by-reference to increase performance, the engine is smart
enough to optimize this on its own."

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация