Posts: 39
Location: Aachen, Germany
|
For those who have no clue what both are:
call by value is the default way to handle parameters in a function. To handle them the copy operator is used so modifying the parameter won't change the original variable passed to the function.
function by_value($a, $b){}
call by reference alters the way parameters are handled. A reference is an alias for the passed variable so changing it will affect the original variable instead of a local copy. Instead of copying a whole lot of data, e.g. objects, call by reference safes a lot of storage due to not copying or, in other words, doubling the merory needed to store the object.
function by_reference(&$a, &$b){}
This beeing said, I'd like to discuss about an advice of Zend Studio 4:
Quote:
|
Contrary to popular belief, this [call by reference] actually reduces performance in most cases [...]
|
I'm using C++ a lot and this statement almost caused a heartattack O_o
The variables I pass are mainly large strings with more than 100 words and large arrays containing information about these large strings. Since PHP is significantly different from C++ I'm interested in the way PHP tries to speed up performance, though I have major doubts that this statement is actually true. The reason for my doubts is the fact that in most cases (in fact anything but small numbers) a pointer to the adress would consume less memory than safing the data. Theoretically everything below 256 bit would qualify for call by value and anything above those 256 bit for a call by reference. The question is, if PHP handles call by value faster than call by referene as than the performance would be better even with larger data (until what size, though?).
So if anyone can clear things up, feel welcome to join the discussion. As a focus I'd like to discuss the behavior of call by value vs call by reference on three major groups:
numbers and strings below 256 bit (I'm assuming a 32 bit adress machine),
strings containing significantly more than 256 Bit (50++ words), multidimensional arrays with a mixture of relativly small numbers and strings,
and objects in PHP 5
Last edited by Recrehal; 05-05-2005 at 04:42 PM..
|