note 28092 modified in control-structures.foreach by vrana

From: Date: Thu, 05 Aug 2004 18:11:38 +0000
Subject: note 28092 modified in control-structures.foreach by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74061@lists.php.net to get a copy of this message
Note that foreach is faster than while! If you can replace the following: <?php reset($array); while(list($key, $val) = each($array)) { $array[$key] = $val + 1; } ?> ...with this (although there are functional differences, but for many purposes this replacement will behave the same way)... <?php foreach($array as $key => $val) { $array[$key] = $val + 1; } ?> You will notice about 30% - 40% speed increase over many iterations. Might be important for those ultra-tight loops :) --was-- Note that foreach is faster than while! If you can replace the following: reset($array); while(list($key, $val) = each($array)) { $array[$key] = $val + 1; } ...with this (although there are functional differences, but for many purposes this replacement will behave the same way)... foreach($array as $key => $val) { $array[$key] = $val + 1; } You will notice about 30% - 40% speed increase over many iterations. Might be important for those ultra-tight loops :) http://php.net/manual/en/control-structures.foreach.php

« previous php.notes (#74061) next »