473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

large string...

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
3 1776
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Scott | last post by:
Hi There. I have a small c# problem. I am trying to create a dynamic email body. The body may be larger than the maximum number of characters a string can have (256?). I build the string like...
2
by: Developwebsites | last post by:
const int MAX=999; class person { protected: char firstname, lastname; int ID; public: person();
3
by: Will McGugan | last post by:
Hi, Is there a simple way of replacing a large number of substrings in a string? I was hoping that str.replace could take a dictionary and use it to replace the occurrences of the keys with the...
9
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
6
by: Peter Hickman | last post by:
I have a program that requires x strings all of y length. x will be in the range of 100-10000 whereas the strings will all be < 200 each. This does not need to be grown once it has been created....
3
by: Bob Maggio | last post by:
I have created a function that returns a string containing raw HTML to be used on a web form (see below): private string GetReportHTML() { string strHTML = ""; // I have some code here that...
1
by: Mark | last post by:
Hi, I've seen some postings on this but not exactly relating to this posting. I'm reading in a large mail message as a string. In the string is an xml attachment that I need to parse out and...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
6
by: Lenny Wintfeld | last post by:
Hi I'm attempting additions/changes to a Java program that (among other things) uses XSLT to transform a large (96 Mb) XML file. It runs fine on small XML files but generates OutOfMemory...
10
by: Jonathan Sion | last post by:
Hi, I got a large text string, and a bunch of regular expression patterns i need to find within the string. in other words, to find all the 'tokens' inside it. of course I could use the regexp...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.