473,651 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1642
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
20352
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 , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference object. I suppose i could return a pointer instead, but i have some code written with references which...
1
2268
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 the TokeyKeys/TokenValues tables and just added a localeid in the regions table, but this is the desired schema by the client. Here's the schema: Table: regions
13
5315
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 returned from the prompt in the statement above, into the value of the statement below ?
18
2135
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 problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
10
10282
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. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
2
2216
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 objects. That's not really that clear, so here's an example: If I have a table named "table_i_would_like_to_delete_but_don't_know_if_any_object_is_using_it",
6
2940
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 will employ a software "Plug-In" architecture. Taking the plug-in route will give us a design that can adapt to, as yet, undefined future requirements (within the scope of the plug-in interface spec of course). In the past I've done this with...
4
3024
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; instead of <strong> and so on. Is this a problem with the editor or something else? Does .Net convert it? I had to disable page validate request because of the tags, is there more? Thanks,
23
2926
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 method. QUOTE ENDS HERE I have never heard anyone else say that it is a problem for a function
0
8357
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8803
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.