Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP (>= 5.0), is passing by reference faster?

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.

like image 271
Hanno Fietz Avatar asked Oct 07 '08 13:10

Hanno Fietz


People also ask

Is pass by reference faster PHP?

In fact, in most scenarios passing by value is faster and less memory intensive than passing by reference. The Zend Engine, PHP's core, uses a copy-on-write optimization mechanism that does not create a copy of a variable until it is modified.

Is PHP pass by value or reference?

It's by value according to the PHP Documentation. By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

Which is better pass by value or pass by reference?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.

Is PHP array pass by reference?

With regards to your first question, the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.


1 Answers

In a test with 100 000 iterations of calling a function with a string of 20 kB, 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

like image 157
ikary Avatar answered Sep 18 '22 16:09

ikary