By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

large string...

P: 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
Jun 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 28 Jun 2006 18:00:49 +0200, "B.r.K.o.N.j.A" <th********@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/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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jun 28 '06 #2

P: n/a
B.r.K.o.N.j.A wrote:
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


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
Jun 28 '06 #3

P: n/a
Mladen Gogala wrote:

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.


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.

Jun 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.