ID:               34540
 Updated by:       [EMAIL PROTECTED]
 Reported By:      php at vicaya dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Performance problem
 Operating System: Linux 2.6.12
 PHP Version:      4.4.0
 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

In case you pass it by reference to the function, and then use the same
variable as parameter to a function that doesn't accept by reference,
then PHP needs to make a copy of the array. In case you do not pass by
reference, in this case PHP doesn't have to copy the array because it's
internal refcounting mechanism is smart enough.
If you are just passing by reference to ensure PHP doesn't create a
copy, then you're doing things wrong. You should only pass by reference
when you are actually modifying the array inside the function.


Previous Comments:
------------------------------------------------------------------------

[2005-09-18 08:18:26] php at vicaya dot com

Description:
------------
There is a dramatic (talking about orders of magnitude here) slow down
when use count or strlen on referenced variables (including pass by
reference and using the global keyword). strace shows excessive brk and
mremap activities in such cases.

If you use the foreach loop (commented out in the attached code) to
count the elements, the version using reference is faster. See attached
code and benchmark for details.

# pass by value and count()
$ /usr/bin/time php passva.php
2.43user 0.30system 0:02.77elapsed 98%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (0major+47193minor)pagefaults 0swaps

# pass by reference and count()
/usr/bin/time php passra.php
70.32user 14.52system 1:27.73elapsed 96%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (0major+2982532minor)pagefaults 0swaps

# pass by value and foreach loop
$ /usr/bin/time php passva.php
127.99user 1.72system 2:13.97elapsed 96%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (0major+276203minor)pagefaults 0swaps

# pass by reference and foreach loop
$ /usr/bin/time php passra.php
47.73user 0.37system 0:49.36elapsed 97%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (0major+47194minor)pagefaults 0swaps


Reproduce code:
---------------
<?
# passva.php
for ($i = 0; $i < 1000000; ++$i) { $a[] = $i; }
for($i = 0 ; $i < 100 ; ++$i ) doit($a);
#function doit($arg) { $s = 0; foreach($arg as $i) { ++$s; } }
function doit($arg) { count($arg); }
?>

<?
# passra.php
for ($i = 0; $i < 1000000; ++$i) { $a[] = $i; }
for($i = 0 ; $i < 100 ; ++$i ) doit($a);
#function doit(&$arg) { $s = 0; foreach($arg as $i) { ++$s; } }
function doit(&$arg) { count($arg); }
?>




------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=34540&edit=1

Reply via email to