what is slower or what is faster in PHP: if( @$myvar['test'] === null ) or if( !isset( $myvar['test'] ))
I wonder what is slower or faster:
if( @$myvar['test'] === null ) { .. }
or:
if( !isset( $myvar['test'] )) { .. }
Also wondering if you suppress a warning or notice with @, will it make the evaluation slower?
Thanks for your answer!
PS: It is not about the difference, i know that isset checks if a ele开发者_Python百科ment is set and not if it is empty or not. But in my case is only important to know if it is empty.
Generally the use of @ does create an overhead in the event of an error condition, so I'd expect it to be slower... and I'd say that using that syntax is less intuitive. Don't try to micro-optimise at the expense of readability
<?
$myvar = array();
$start = microtime(true);
for($x=0;$x<100000;$x++){
if( @$myvar['test'] === null ) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test 1: %s \n", $duration);
$start = microtime(true);
for($x=0;$x<100000;$x++){
if( !isset( $myvar['test'] )) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test 2: %s \n", $duration);
// populate
$myvar['test'] = true;
$start = microtime(true);
for($x=0;$x<100000;$x++){
if( @$myvar['test'] === null ) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test 3: %s \n", $duration);
$start = microtime(true);
for($x=0;$x<100000;$x++){
if( !isset( $myvar['test'] )) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test 4: %s \n", $duration);
Result:
Test 1: 0.18865299224854
Test 2: 0.012698173522949
Test 3: 0.11134600639343
Test 4: 0.015975952148438
Suppressing warnings with @ does slow down things. isset()
should be the way to go here.
As an aside, I remember reading some tip that claims that strict testing with ===
is actually marginally quicker than ==
in PHP it doesn't matter
Only one way to find out for sure - test it.
精彩评论