Forum Moderators: coopster

Message Too Old, No Replies

Pass by reference vs pass by value

speed?

         

Code Sentinel

8:14 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



How big does an array have to be before passing to functions by reference is faster?

So far in my tests I've found pass by value faster, although my array is quite small... and I'm on a windows machine for testing if that makes any difference.

mcibor

9:05 pm on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I recall passing by value is always faster.
However passing by reference is used, so you don't use global, which are very confusing in reading the code (you don't know if the function changes that value or not just from seeing the call).

Therefore I use reference where I need global values and otherwise plain value.

Ergophobe is very good in cases of speed, so you can either wait for him to answer, or write directly to him.

Best regards
michal Cibor

ergophobe

10:24 pm on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Ergophobe is very good in cases of speed

Not really, but when I'm bored, sometimes I make up short test scripts and run them through xdebug and CacheGrind, but this is one I've never tried.

Anyway, CS, the problem with your test case is that it does not take into account the PHP4 reference counting implementation. Because of that, there is really not much difference between passing by reference and passing by value in terms of performance up until the point that you start to modify elements of the array.

In other words, PHP4 is essentially passing by reference in all cases (except in the case of objects, but they get passed by reference in PHP5+). The difference between explicitly passing by reference and implicitly doing so via reference counting is what happens *after* the function call when you start manipulating data. With an explicit pass by reference, the original array will get modified. Otherwise, the array local to the function gets modified.

In other words, when I do this

$a = array(1,2,3,4,5,6,7,8,9);
$b = $a;

I'm not actually making a copy. I'm actually making a second variable that points to the same location in memory as the first variable, essentially as if it were a reference, and incrementing the reference count for that memory location by one.

See [zend.com...] for more info, but in your particular case, the relevant part is:


Note that PHP 4 is not like C: passing variables by reference is not necessarily faster than passing them by value. Indeed, in PHP 4 it is usually better to pass a variable by value, except if the function changes the passed value or if a reference (or alias, see Aliasing: added language flexibility) is being passed.

Objects are usually better passed by reference unless you want copies to be made.

Note - that final line does not hold true for PHP5+

Code Sentinel

11:21 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



now I feel like an idiot.. I forgot why I was passing by reference in the first place.

in short I was inserting nothing into the database which probably was the reason I was saving a tiny bit of execution time compared to the properly working script. :P

ergophobe

12:27 am on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Because of the way PHP works, it doesn't sound like you should be passing by reference in this case (unless you're using PHP 3!).

The only reason to explicitly pass by reference is that you want the function to modify the values of the original array rather than return an array. If you are just passing an array that gets parcelled up into INSERT or UPDATE statements, there shouldn't be any performance-based reason to pass by reference, no matter how big the array is. Again, objects prior to PHP5 are another matter.

Code Sentinel

5:38 am on Jul 23, 2005 (gmt 0)

10+ Year Member



That's what I was doing, creating an array then retreiving data then performing a few functions which would modify or add data to the array, then send it to the DB.

ergophobe

4:56 pm on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes but were you doing A, B or C?

A.


$array = (1,2,3,4,5,6);
array_mod($array);
db_update($array);

function array_mod(&$array)
{
// do stuff to array elements
}

B.


$array = (1,2,3,4,5,6);
db_update($array);

function db_update($array)
{
// do stuff to array elements
// insert into DB
}

C.


$array = (1,2,3,4,5,6);
$new_array = array_mod($array);
db_update($new_array);

function array_mod($array)
{
// do stuff to array elements
}

A is the only case where you would want to explicitly pass by reference, and not for performance reasons. I also think A has the least readable code and would likely be my last choice of how to approach this.

Code Sentinel

3:43 am on Jul 24, 2005 (gmt 0)

10+ Year Member



it was A

mcibor

8:34 pm on Jul 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ergophobe, if you do A like this it is more readable:

A

$array = (1,2,3,4,5,6);
array_mod(&$array);
db_update($array);

function array_mod($array)
{
// do stuff to array elements
}

I found this works when I enquired about reference. Here you see perfectly clear what is and what is not passed by reference:)
Clear enough for me

See you round
Michal Cibor

dcrombie

8:48 am on Jul 25, 2005 (gmt 0)



mcibor, I think that method of passing by reference has been deprecated in PHP5. The '&' now needs to be in the function definition.

ergophobe

4:15 pm on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



dcrombie is right. You will get a "run-time call by reference" error (or warning perhaps).

The idea is that the function definition should determine the tyep of data expected. This should be part of the interface and not left to the discretion of those who write code that interacts with the interface.