#34540 [NEW]: dramatic count and strlen slow down on variable reference

From: Date: Sun, 18 Sep 2005 06:18:29 +0000
Subject: #34540 [NEW]: dramatic count and strlen slow down on variable reference
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-85200@lists.php.net to get a copy of this message
From: php at vicaya dot com Operating system: Linux 2.6.12 PHP version: 4.4.0 PHP Bug Type: Performance problem Bug description: dramatic count and strlen slow down on variable reference 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 bug report at http://bugs.php.net/?id=34540&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34540&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=34540&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=34540&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=34540&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=34540&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=34540&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=34540&r=needscript Try newer version: http://bugs.php.net/fix.php?id=34540&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=34540&r=support Expected behavior: http://bugs.php.net/fix.php?id=34540&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=34540&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=34540&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=34540&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34540&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=34540&r=dst IIS Stability: http://bugs.php.net/fix.php?id=34540&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=34540&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=34540&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=34540&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=34540&r=mysqlcfg

« previous php.bugs (#85200) next »