#25855 [NEW]: move stuff out of the loop as much as possible

From: Date: Mon, 13 Oct 2003 19:28:17 +0000
Subject: #25855 [NEW]: move stuff out of the loop as much as possible
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-48934@lists.php.net to get a copy of this message
From: katsura at apple dot com Operating system: Mac OS X 10.3 PHP version: 4.3.3 PHP Bug Type: Performance problem Bug description: move stuff out of the loop as much as possible Description: ------------ 10/13/03 12:14 PM Hidetomo Katsura: apache_mod_php: move stuff out of the loop as much as possible in this example, CFArrayGetCount() gets executed every time it loops: for ( index = 0; index < CFArrayGetCount( array ); ++index ) { value = CFArrayGetValueAtIndex( array, index ); // do something with value } _if_ the count of array does not change in the loop, CFArrayGetCount() only needs to be called once outside of the loop: count = CFArrayGetCount( array ); for ( index = 0; index < count; ++index ) { value = CFArrayGetValueAtIndex( array, index ); // do something with value } % cd ~rc/Updates/Panther/Panther7B85/Projects/ apache_mod_php % grep -rns "for.*;.*Count(.*;" . ------------ apache_mod_php -------------- ./php/ext/fbsql/php_fbsql.c:3512: for (i=0; i<fbaCount(asInfo->infoLines); i++) { NOTE: this bug is not about major performance improvement, and this bug should be the lowest priority. we all know this is a micro optimization and not going to make Mac OS X 100x faster if you just move the "count" function out of the loop. but the point is that you should never write a loop with unnecessary code in it in the first place. this is just a reminder based on something "srcsgrep" found. please fix whenever you have a chance to modify the listed files. thanks. -- Edit bug report at http://bugs.php.net/?id=25855&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=25855&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=25855&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=25855&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=25855&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=25855&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=25855&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=25855&r=support Expected behavior: http://bugs.php.net/fix.php?id=25855&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=25855&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=25855&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=25855&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25855&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=25855&r=dst IIS Stability: http://bugs.php.net/fix.php?id=25855&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=25855&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=25855&r=float

« previous php.bugs (#48934) next »