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

PhpPerformancePass by-Reference

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

I ran some test on this because I was unsure of the answers given.

My results show that passing large arrays or strings by reference IS significantly faster.

Here are my results: Benchmark

The Y axis (Runs) is how many times a function could be called in 1 second * 10

The test was repeated 8 times for each function/variable

And here is the variables I used:

$large_array = array_fill(PHP_INT_MAX / 2, 1000, 'a');
$small_array = array('this', 'is', 'a', 'small', 'array');
$large_object = (object)$large_array;
$large_string = str_repeat('a', 100000);
$small_string = 'this is a small string';
$value = PHP_INT_MAX / 2;

These are the functions:

function pass_by_ref(&$var) {
}

function pass_by_val($var) {
}

Solution 4 - Php

I have experimented with values and references of 10k bytes string passing it to two identical function. One takes argument by value and the second one by reference. They were common functions - take argument, do simple processing and return a value. I did 100 000 calls of both and figured out that references are not designed to increase performance - profit of reference was near 4-5% and it grows only when string becomes large enough (100k and longer, that gave 6-7% improvement). So, my conclusion is do not use references to increase perfomance, this stuff is not for that.

I used PHP Version 5.3.1

Solution 5 - Php

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!

Solution 6 - Php

I tried to benchmark this with a real-world example based on a project I was working on. As always, the differences are trivial, but the results were somewhat unexpected. For most of the benchmarks I've seen, the called function doesn't actually change the value passed in. I performed a simple str_replace() on it.

**Pass by Value Test Code:**

$originalString=''; // 1000 pseudo-random digits

function replace($string) {
    return str_replace('1', 'x',$string);
}
$output = '';
/* set start time */
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$tstart = $mtime;
set_time_limit(0);

for ($i = 0; $i < 10; $i++ ) {
    for ($j = 0; $j < 1000000; $j++) {
        $string = $originalString;
        $string = replace($string);
    }
}

/* report how long it took */
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$tend = $mtime;
$totalTime = ($tend - $tstart);
$totalTime = sprintf("%2.4f s", $totalTime);
$output .= "\n" . 'Total Time' .
    ': ' . $totalTime;
$output .= "\n" . $string;
echo $output;

Pass by Reference Test Code

The same except for

function replace(&$string) {
    $string = str_replace('1', 'x',$string);
}
/* ... */
replace($string);

Results in seconds (10 million iterations):

PHP 5
    Value:     14.1007
    Reference: 11.5564

PHP 7
    Value:     3.0799
    Reference: 2.9489

The difference is a fraction of a millisecond per function call, but for this use case, passing by reference is faster in both PHP 5 and PHP 7.

(Note: the PHP 7 tests were performed on a faster machine -- PHP 7 is faster, but probably not that much faster.)

Solution 7 - Php

There is nothing better than a testing piece of code

<?PHP
$r = array();

for($i=0; $i<500;$i++){
$r[]=5;
}

function a($r){
$r[0]=1;
}
function b(&$r){
$r[0]=1;
}

$start = microtime(true);
for($i=0;$i<9999;$i++){
  //a($r);
  b($r);
}
$end = microtime(true);

echo $end-$start;
?>

Final result! The bigger the array (or the greater the count of calls) the bigger the difference. So in this case, calling by reference is faster because the value is changed inside the function.

Otherwise there is no real difference between "by reference" and "by value", the compiler is smart enough not to create a new copy each time if there is no need.

Solution 8 - Php

Is simple, there is no need to test anything. Depends on use-case.

Pass by value will ALWAYS BE FASTER BY VALUE than reference for small amount of arguments. This depends by how many variables that architecture allows to be passed through registers (ABI).

For example x64 will allow you 4 values 64 bit each to be passed through registers. https://en.wikipedia.org/wiki/X86_calling_conventions

This is because you don't have to de-referentiate the pointers, just use value directly.

If your data that needs to be passed is bigger than ABI, rest of values will go to stack. In this case, a array or a object (which in instance is a class, or a structure + headers) will ALWAYS BE FASTER BY REFERENCE.

This is because a reference is just a pointer to your data (not data itself), fixed size, say 32 or 64 bit depending on machine. That pointer will fit in one CPU register.

PHP is written in C/C++ so I'd expect to behave the same.

Solution 9 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHanno FietzView Question on Stackoverflow
Solution 1 - PhpikaryView Answer on Stackoverflow
Solution 2 - PhpPaul DixonView Answer on Stackoverflow
Solution 3 - PhpPetahView Answer on Stackoverflow
Solution 4 - PhpVladimir FeskoView Answer on Stackoverflow
Solution 5 - PhpGregView Answer on Stackoverflow
Solution 6 - PhpBob RayView Answer on Stackoverflow
Solution 7 - PhpMelsiView Answer on Stackoverflow
Solution 8 - PhporfruitView Answer on Stackoverflow
Solution 9 - PhpMichał NiedźwiedzkiView Answer on Stackoverflow