Optimization with foreach() an empty array

<?php
$vResult = array();
$vAry = array(‘abc’);
$vLimit = 10000000;

$vStart = microtime(true);
for ($i=1;$i<$vLimit; $i++) {
foreach ($vAry as $a) {}
} // rof
$vResult[] = “NoEmptyCheck= “.(microtime(true)-$vStart);

$vStart = microtime(true);
for ($i=1;$i<$vLimit; $i++) {
if (!empty($vAry)) {
foreach ($vAry as $a) {}
}
} // rof
$vResult[] = “WithEmptyCheck= “.(microtime(true)-$vStart);

echo implode(“\n”,$vResult).”\n”;

/* Result

For an often-non-empty array scenario:
NoEmptyCheck= 2.10900402069
WithEmptyCheck= 2.59749412537

For an often-empty array scenario:
– NoEmptyCheck= 1.08695101738
– WithEmptyCheck= 1.01621007919

*/

?>

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.