note 27272 added to language.types.array

From: Date: Fri, 29 Nov 2002 21:10:28 +0000
Subject: note 27272 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-40130@lists.php.net to get a copy of this message
Dereferencing arrays takes some time, but is not terribly expensive. I wrote two dummy loops to test performance: for ($i =0; $i < count($a); $i++) { $x = $a[$b[$i]]; $y = $a[$b[$i]]; $z = $a[$b[$i]]; } for ($i =0; $i < count($a); $i++) { $q = $b[$i]; $x = $a[$q]; $y = $a[$q]; $z = $a[$q]; } The first loop is 6.5% slower than the second. Meaning that dereferencing arrays is not terribly expensive, unless you do it a whole lot. I would expect that each extra reference costs about 3% in speed. The lesson is that if you're going to be using a specific value in an array for a number of operations, you can gain a little speed by assigning it to a temp variable (or creating a reference with $q = &$b[$i]) but it's not worth getting crazy over. I tested this with iterations of 10,000 and 100,000 on php 4.2 and the results were consistent. -- http://www.php.net/manual/en/language.types.array.php http://master.php.net/manage/user-notes.php?action=edit+27272 http://master.php.net/manage/user-notes.php?action=delete+27272 http://master.php.net/manage/user-notes.php?action=reject+27272

« previous php.notes (#40130) next »