473,406 Members | 2,707 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,406 software developers and data experts.

Why Returning References can't provide speed up?

from PHP manual, it said:
>http://hk.php.net/manual/tw/language...ces.return.php
Do not use return-by-reference to increase performance, the engine is
smart enough to optimize this on its own

------------------

Why?

The bottom line from my view in using reference in all my codes are :

1. memory allocation need time! why not use reference?
2. you waste memory for storing two copies of data

Return-by-reference should at least faster, more efficient, so I can't
find a reason not using it.

Feb 8 '07 #1
8 1632
Rik
howa <ho******@gmail.comwrote:
from PHP manual, it said:
>>http://hk.php.net/manual/tw/language...ces.return.php

Do not use return-by-reference to increase performance, the engine is
smart enough to optimize this on its own

------------------

Why?
in case of

function foo(){
$bar = 'somestring';
return $bar;
}

The engine already knows it should be returned by reference, and does
this. This would be plain silly:

function foo(){
$bar = 'somestring';
return &$bar;
}

As the engine already knows and does this.

Actually, a lot of assignments are treated as references untill you try to
change one of the references, and only then is a copy made & assigned.

In other words: the engine is smarter then you think.
--
Rik Wasmus
Feb 8 '07 #2
howa wrote:
Do not use return-by-reference to increase performance, the engine is
smart enough to optimize this on its own

------------------

Why?
Please re-read the above sentence. Specifically, re-read the part about "the
engine is smart enough to optimize this on its own".
Return-by-reference should at least faster, more efficient, so I can't
find a reason not using it.
Code cleanliness. If the Zend Engine is capable of returning references as
needed, why bother keeping track of what is a reference and what is not?

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

"kill -9 needs no justification!"
-- BOFH
Feb 8 '07 #3
..oO(howa)
>from PHP manual, it said:
>>http://hk.php.net/manual/tw/language...ces.return.php

Do not use return-by-reference to increase performance, the engine is
smart enough to optimize this on its own

------------------

Why?

The bottom line from my view in using reference in all my codes are :

1. memory allocation need time! why not use reference?
2. you waste memory for storing two copies of data
Memory is usually not an issue on recent machines.
>Return-by-reference should at least faster, more efficient, so I can't
find a reason not using it.
Not necessarily. It depends on how references and object copies are
handled internally. The engine itself knows better than you how to
optimize such things.

It's like trying to "over-optimize" an SQL query, where the built-in
query optimizer could do a much better job.

Micha
Feb 8 '07 #4
In other words: the engine is smarter then you think.
--
the bottom line is: the engine created dummy variables for storing two
copies of data, e.g.
<?php

function conv_md5( $data ) {

return md5($data);
}

$data = "abc";

echo conv_md5( $data );
echo $data;
?>

the engine never know we will use the variable $data later, so it
always created allocated memory for storing the result of conv_md5(),
only we know and can force to use return by reference.

Feb 9 '07 #5
Rik
howa <ho******@gmail.comwrote:
>In other words: the engine is smarter then you think.
--

the bottom line is: the engine created dummy variables for storing two
copies of data, e.g.
<?php

function conv_md5( $data ) {

return md5($data);
}

$data = "abc";

echo conv_md5( $data );
echo $data;
?>

the engine never know we will use the variable $data later, so it
always created allocated memory for storing the result of conv_md5(),
only we know and can force to use return by reference.
No, it is not assigned to anything. The results are echoed and discarded,
you are not using it's results again as you have not assigned it anywhere.
Any variables within a function's scope are discarded on exit (unless
declared static).

_returning_ by reference here would not change the global $data.

However, the following would make sense:
<?php
function foo(&$data){
$data = md5($data);
}
$bar = 'something';
foo($bar);
echo $bar;
?>

But that's not returning by reference, that's passing by reference.
--
Rik Wasmus
Feb 9 '07 #6
>
No, it is not assigned to anything. The results are echoed and discarded,
you are not using it's results again as you have not assigned it anywhere.
Any variables within a function's scope are discarded on exit (unless
declared static).
yes, it is echoed and discarded, but my point is:

you need to allocate extra memory for storing the output of md5, so
return by reference also make sense. (as well as your one above)

Feb 9 '07 #7
Rik
On Fri, 09 Feb 2007 16:14:30 +0100, howa <ho******@gmail.comwrote:
>
>>
No, it is not assigned to anything. The results are echoed and
discarded,
you are not using it's results again as you have not assigned it
anywhere.
Any variables within a function's scope are discarded on exit (unless
declared static).

yes, it is echoed and discarded, but my point is:

you need to allocate extra memory for storing the output of md5, so
return by reference also make sense. (as well as your one above)
Return by reference does not make sense because the engine already does
this internally. What's it about 'the engine already does this' that you
do not understand?
--
Rik Wasmus
Feb 9 '07 #8
..oO(howa)
>yes, it is echoed and discarded, but my point is:

you need to allocate extra memory for storing the output of md5, so
return by reference also make sense. (as well as your one above)
A reference also requires memory, so what? Such "over-optimization",
especially when nitpicking on single bytes of memory, usually just
makes things worse, because you don't know the exact internal handling
of variables and references. But the engine does, so you should let her
do their job.

Micha
Feb 9 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
1
by: Justin | last post by:
Hi, In the process of localizing the 'regions' table, we added three new tables. The localized data will be stored in the TokenKeys and TokenValues tables. It would be easier if we did away with...
13
by: JSE | last post by:
Hello everyone. here's what we have in an html file. <form ..... <input type=button name="comment" onclick=" prompt('enter comment',' ' )" > Is there anyway to get the value...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
2
by: Jonathan LaRosa | last post by:
Hi all - I'm wondering if anyone has (or knows of) a tool that will allow me to search through VB code, tables, queries, reports, forms, and other objects, for references to all other types of...
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
4
by: David Lozzi | last post by:
Howdy, I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then save it back to my SQL server using a SQLCommand, all HTML characters are changed to HTML code, i.e. &gt;strong&lt;...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.