note 103314 deleted from function.array-filter by danbrown
| From: | danbrown@php.net | Date: | Wed, 06 Apr 2011 23:54:32 +0000 |
| Subject: | note 103314 deleted from function.array-filter by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178577@lists.php.net to get a copy of this message | ||
Note Submitter: rmaslo
----
benchmark
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function odd($var)
{
return($var & 1);
}
$maxarr=50000;
for ($i=0; $i<$maxarr; $i++ ) $bigarr[$i]=rand(0,999999);
$t1=microtime_float();
$x1 = array_filter($bigarr, "odd");
$t2=microtime_float();
foreach($bigarr as $k=>$v) if (odd($v)) $x3[]=$v;
$t3=microtime_float();
foreach($bigarr as $k=>$v) if ($v & 1) $x3[]=$v;
$t4=microtime_float();
for ($i=0; $i<$maxarr; $i++ ) if (odd($bigarr[$i])) $x4 = $bigarr[$i];
$t5=microtime_float();
for ($i=0; $i<$maxarr; $i++ ) if ($bigarr[$i] & 1) $x5 = $bigarr[$i];
$t6=microtime_float();
echo (($t2-$t1)*100).' .... array_filter <BR>';
echo (($t3-$t2)*100).' .... foreach + func<BR>';
echo (($t4-$t3)*100).' .... func in foreach<BR>';
echo (($t5-$t4)*100).' .... for + func<BR>';
echo (($t6-$t5)*100).' .... func in for<BR>';
-------------------
4.90231513977 .... array_filter
5.48429489136 .... foreach + func
2.20520496368 .... func in foreach
5.27548789978 .... for + func
1.67551040649 .... func in for