note 34178 added to control-structures.for

From: Date: Thu, 17 Jul 2003 18:23:29 +0000
Subject: note 34178 added to control-structures.for
Groups: php.notes 
Request: Send a blank email to php-notes+get-52290@lists.php.net to get a copy of this message
If you're already using the fastest algorithms you can find (on the order of O(1), O(n), or O(n log n)), and you're still worried about loop speed, unroll your loops using e.g., Duff's Device: $n = $ITERATIONS % 8; while ($n--) $val++; $n = (int)($ITERATIONS / 8); while ($n--) { $val++; $val++; $val++; $val++; $val++; $val++; $val++; $val++; } (This is a modified form of Duff's original device, because PHP doesn't understand the original's egregious syntax.) That's algorithmically equivalent to the common form: for ($i = 0; $i < $ITERATIONS; $i++) { $val++; } $val++ can be whatever operation you need to perform ITERATIONS number of times. On my box, with no users, average run time across 100 samples with ITERATIONS = 10000000 (10 million) is: Duff version: 7.9857 s Obvious version: 27.608 s ---- Manual Page -- http://www.php.net/manual/en/control-structures.for.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34178 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34178&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34178&report=yes

« previous php.notes (#52290) next »