+ 415 % 1: foreach($aHash as $val); Total time: 6[ms]
+ 204 % 2: while(list(,$val) = each($aHash)); Total time: 3[ms]
+ 853 % 3: foreach($aHash as $key=>$val); Total time: 13[ms]
+ 926 % 4: while(list($key,$val)= each($aHash));
if you have only one val, while -list *MIGHT* be faster, but it would be good to test that.
Here are my tests:
hirako:~/test# php test.php 100000
Test foreach took 0.045385ms.
Test foreach-ass took 0.065821ms.
Test for took 0.073692ms.
Test while took 0.069795ms.
Test while-list-each(ass) took 0.240931ms.
Test while-list-each(val) took 0.21557ms.
this is under PHP5, Zend, No accelerator, uncached.
The php source code was:
hirako:~/test# cat test.php
<?php
$tries=(int)$argv[1];
if ($tries==0) $tries=100000;
$ass=array();
for ($i=0;$i<$tries;$i++) {
$ass["Ad$i"]="Test$i";
}
$num=array();
for ($i=0;$i<$tries;$i++) {
$num[]="Test$i";
}
$test= array(
"foreach"=>'reset($ass);foreach ($ass as $bla) {$b=0;}',
"foreach-ass"=>'reset($ass);foreach ($ass as $key=>$val) {$b=0;}',
"for"=>'for ($i=0;$i<$tries;$i++) {$num[$i]="x";}',
"while"=>'$i=0; while ($i<$tries) {$num[$i]="y";$i++;}',
"while-list-each(ass)"=>'reset($ass);while (list($key,$val)=each($ass)) {$b=0;}',
"while-list-each(val)"=>'reset($ass);while (list(,$val)=each($ass)) {$b=0;}',
);
foreach ($test as $name=>$code) {
$start=microtime();
eval($code);
$end=microtime();
echo "Test ".$name." took ".($end-$start)."ms.\n";
}