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

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

Leave a Comment