In PHP (> = 5.0) is the link faster?

In PHP, function parameters can be passed by reference, adding an ampersand to the parameter in the function declaration, for example:

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

Now I know that this is not intended to improve performance, but allowing functions to modify variables that usually go out of their scope.

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

However, I was wondering if the "Copy by write" logic can be truncated shortly by link to link and whether this affects performance.

ETA: Of course, I guess this is not faster, and I well know that this is not what links are for. Therefore, I think that my own guesses are not bad, I'm just looking for an answer from someone who really knows what exactly is happening under the hood. Over the five years of PHP development, it has always been difficult for me to get quality information about the internal functions of PHP, apart from the source.

+55
performance pass-by-reference php
Oct 07 '08 at 13:04
source share
7 answers

Zend Engine uses copy-on-write, and when you use the link yourself, it takes a little extra overhead. You can find this mention at the time of writing, and the comments in the manual contain other links.

(EDIT) The Objects and Links manual page provides a bit more information on how object variables differ from links.

+30
Oct 07 '08 at 13:14
source share

In a test with 100,000 iterations of a function call with a string of 20 kB, the results are:

A function that simply reads / uses a parameter

 pass by value: 0.12065005 seconds pass by reference: 1.52171397 seconds 

Function for recording / changing a parameter

 pass by value: 1.52223396 seconds pass by reference: 1.52388787 seconds 



findings

  • Passing a parameter by value is always faster

  • If a function changes the value of the passed variable, for practical purposes it is the same as passing by reference than by value

+59
Oct 02 '10 at
source share

I did some testing on this because I was not sure about the answers.

My results show that passing large arrays or strings by IS link is much faster.

Here are my results: Benchmark

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

The test was repeated 8 times for each function / variable

And here are the variables that 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 following functions:

 function pass_by_ref(&$var) { } function pass_by_val($var) { } 
+24
Jul 05 2018-11-11T00:
source share

I'm sure not, it's not faster. In addition, the manual specifically states that links should not be used to improve performance.

Edit: I can’t find where he is talking, but he is there!

+4
07 Oct '08 at 13:06
source share

I experimented with the values ​​and references of a string 10 kilobytes long, passing it to two identical functions. One takes an argument by value, and the second by reference. They were common functions β€” accepting arguments, doing simple processing, and returning a value. I made 100,000 calls to both of them and found out that the links are not designed to increase productivity - the profit from the link is about 4-5%, and it grows only when the line becomes large enough (100 thousand and more, which gives an improvement of 6 -7%), So, my conclusion does not use links to increase performance, this material is not for this.

I used PHP Version 5.3.1

+4
Oct 01 '11 at 2:05 a.m.
source share

There is nothing better than a test 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 larger the array (or the greater the number of calls), the greater the difference. Therefore, in this case, the call by reference is faster, because the value changes 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 every time if there is no need.

+1
Jul 26 '14 at 21:34
source share

When transferring objects, there is no need to add and execute an operator. In PHP 5+, objects are still passed by reference.

-2
Oct 07 '08 at 13:17
source share



All Articles