Month: March 2016
-
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”; /*…
-
Optimization with in_array()
Ever wonder with many in_array() checks, you can speed it up depending on your scenario. In my case, 99% it’s empty so it’s better with an empty() check <?php $vResult = array(); $vAry = array(); $vLimit = 10000000; $vStart = microtime(true); for ($i=1;$i<$vLimit; $i++) { if (in_array(123,$vAry)) { } } // rof $vResult[] = “NoEmptyCheck=…