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

Why Returning References can't provide speed up?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
..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

P: n/a
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

P: n/a
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

P: n/a
>
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

P: n/a
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

P: n/a
..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 discussion thread is closed

Replies have been disabled for this discussion.