|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-09-18 20:13 UTC] derick@php.net
[2005-09-18 22:54 UTC] php at vicaya dot com
[2013-05-12 16:25 UTC] sergio dot nalin at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 01:00:02 2025 UTC |
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); } ?>