Welcome Guest, Not a member yet? Register   Sign In
Checking a variable for existance without isset(), empty(), or any other function
#7

[eluser]BrianDHall[/eluser]
@chad Fulton, thanks so much for your performance test and insight. I tried a slightly expanded and modified version:

Code:
$num =100000;
$tmp = array();

for ($i=0;$i<($num/2);$i++) {
    $tmp[$i]= "simple element $i";
}
reset($tmp);

ini_set('display_errors', 0);
error_reporting(0);


$t1 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(isset($tmp[$i]) && $tmp[$i]);
}
reset($tmp);
$t2 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(array_key_exists($i,$tmp) && $tmp[$i]);
}
reset($tmp);
$t3 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if($tmp[$i]);
}
reset($tmp);
$t4 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(@$temp[$i]);
}
reset($tmp);
$t5 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(empty($tmp[$i]));
}
reset($tmp);
$t6 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(! empty($tmp[$i]));
}
reset($tmp);
$t7 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(true);
}
reset($tmp);
$t8 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if($temp[$i]);
}
reset($tmp);
$t9 = microtime(true);
for ($i=0;$i<$num;$i++) {
    if(@$temp[$i]);
}
reset($tmp);
$t10 = microtime(true);

echo 'Isset: '.($t2-$t1).'<br />';
echo 'Array Key Exists: '.($t3-$t2).'<br />';
echo 'Neither: '.($t4-$t3).'<br />';
echo 'Neither w/ @: '.($t5-$t4).'<br />';
echo 'Empty: '.($t6-$t5).'<br />';
echo 'NOT Empty: '.($t7-$t6).'<br />';
echo 'Boolean: '.($t8-$t7).'<br />';
echo 'Cause Errors: '.($t9-$t8).'<br />';
echo 'Cause Errors w/ @: '.($t10-$t9).'<br />';

Note: All results are shown as a floating point in seconds.

Localhost WAMP:
Quote:Isset: 0.088625907897949
Array Key Exists: 0.4364640712738
Neither: 2.6649811267853
Neither w/ @: 5.2826218605042
Empty: 0.070709943771362
NOT Empty: 0.084458112716675
Boolean: 0.073637962341309
Cause Errors: 5.3429410457611
Cause Errors w/ @: 5.3846030235291

Live LAMP server:
Quote:Isset: 0.0335500240326
Array Key Exists: 0.0874769687653
Neither: 0.855342149734
Neither w/ @: 1.74126601219
Empty: 0.0294389724731
NOT Empty: 0.0313649177551
Boolean: 0.0208189487457
Cause Errors: 1.76280021667
Cause Errors w/ @: 1.82197380066

Wow, those last two I added are calling to a non-existent variable $temp instead of $tmp.

Indeed, it is clear the performance effect of errors (here adding as much as 1-2 seconds of run time on a single page access just to deal internally with the generated errors, or even just chance of errors). Now add that to a slightly busy site on a slightly busy server, and the fact that this is a basic coding convention that might be done dozens of times in any given function, with dozens of function calls, etc etc - that could add up DAMN quick, especially when dealing with arrays of increasing size and complexity!

As much as I like the perl-like single character use instead of a function call, here the performance is too big for even me to ignore - going back to empty() and isset() Smile

Indeed, it appears even asking if() to evaluate anything other than a boolean has a significant performance impact when looping through an array - even if you KNOW the variable is initialized!


Messages In This Thread
Checking a variable for existance without isset(), empty(), or any other function - by El Forum - 11-24-2009, 12:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB