views:

1022

answers:

4

In PHP, function parameters can be passed by reference by prepending an ampersand to the parameter in the function declaration, like so:

function foo(&$bar)
{
    // ...
}

Now, I am aware that this is not designed to improve performance, but to allow functions to change variables that are normally out of their scope.

Instead, PHP seems to use Copy On Write to avoid copying objects (and maybe also arrays) until they are changed. So, for functions that do not change their parameters, the effect should be the same as if you had passed them by reference.

However, I was wondering if the Copy On Write logic maybe is shortcircuited on pass-by-reference and whether that has any performance impact.

ETA: To be sure, I assume that it's not faster, and I am well aware that this is not what references are for. So I think my own guesses are quite good, I'm just looking for an answer from someone who really knows what's definitely happening under the hood. In five years of PHP development, I've always found it hard to get quality information on PHP internals short from reading the source.

+2  A: 

I'm pretty sure that no, it's not faster. Additionally, it says specifically in the manual not to try using references to increase performance.

Edit: Can't find where it says that, but it's there!

Greg
+4  A: 

The Zend Engine uses copy-on-write, and when you use a reference yourself, it incurs a little extra overhead. Can only find this mention at time of writing though, and comments in the manual contain other links.

(EDIT) THe manual page on Objects and references contains a little more info on how object variables differ from references.

Paul Dixon
So you're saying that it actually hurts performance (even though maybe the impact is very insignificant)? That's interesting, thanks!
Hanno Fietz
The (somewhat academic) overall performance loss should occur only if you never manipulate the original data structure. When you plan to, you should actually *gain* performance instead because you avoid the copy-on-write.
Tomalak
Yeah, granted, this is a little academic. It's just that I've had a lot of frustration from misunderstanding how PHP internally works and that made me a bit pedantic about finding out. It seems to me that good sources on PHP internals are harder to find than with other languages, e.g. Python
Hanno Fietz
+1  A: 

There is no need for adding & operator when passing objects. In PHP 5+ objects are passed by reference anyway.

Michał Rudnicki
Or, actually, the way they are represented has been changed so that what's being passed around is always just a handler/reference/pointer anyway. But that wasn't exactly my question.
Hanno Fietz
+1  A: 

in a test 100000 iterations of calling a function with a string of 20kb, the results are:

  • Function that just reads / uses the parameter

pass by value: 0.12065005 seconds

pass by reference: 1.52171397 seconds

  • Function to write / change the parameter

pass by value: 1.52223396 seconds

pass by reference: 1.52388787 seconds


conclusions:

1 .- pass the parameter by value is always faster

2 .- if the function change the value of the variable passed, for practical purposes is the same as pass by reference than by value