Connecting Tech Pros Worldwide Help | Site Map

large string...

  #1  
Old June 28th, 2006, 05:05 PM
B.r.K.o.N.j.A
Guest
 
Posts: n/a
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

Best regards,
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination
  #2  
Old June 28th, 2006, 06:55 PM
Andy Hassall
Guest
 
Posts: n/a

re: large string...


On Wed, 28 Jun 2006 18:00:49 +0200, "B.r.K.o.N.j.A" <thebrkonja@inet.hr> wrote:
[color=blue]
>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[/color]

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/languag...ces.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
  #3  
Old June 28th, 2006, 07:15 PM
Mladen Gogala
Guest
 
Posts: n/a

re: large string...


B.r.K.o.N.j.A wrote:
[color=blue]
> 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[/color]

Andy replied below so I will not repeat his answer but will only add
that you can explicitly declare arguments to be accessed by reference, as in

function myfunct(&$foobar)

Also, global variables can be used to minimize stack manipulation.

--
Mladen Gogala
http://www.mgogala.com
  #4  
Old June 28th, 2006, 09:25 PM
Chung Leong
Guest
 
Posts: n/a

re: large string...


Mladen Gogala wrote:[color=blue]
>
> Andy replied below so I will not repeat his answer but will only add
> that you can explicitly declare arguments to be accessed by reference, as in
>
> function myfunct(&$foobar)
>
> Also, global variables can be used to minimize stack manipulation.[/color]

It's worth noting that references tend to cause more data copying. When
a referenced variable is passed by value, copy-on-write occurs. When a
"copy" of a value is passed by value, it does not unless the function
called modifies its parameters.

Global variables used within functions are referenced in PHP 4, so the
same rules apply.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp .net 2.0 web service post large string phaniva@gmail.com answers 0 October 24th, 2006 04:05 PM
Generating very large string in C# Dukkov@yahoo.com answers 16 June 14th, 2006 09:55 AM
Splitting a large string variable into lines <= 70 chars Daren answers 15 February 27th, 2006 05:05 PM
Large string constant causes C# compiler crash Gnik answers 11 November 16th, 2005 02:49 PM