Use empty function or direct comparison (with undefined variable)

Submitted by Lito on Tue, 03/27/2012 - 12:48

f2 is slower than f1 by 427.98% (4.3 times slower)

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',10000000);
 
function f1() {
	for($i=0; $i<LOOP; ++$i) {
		if (empty($a)) {
			echo '';
		}
	}
}
function f2() {
	for($i=0; $i<LOOP; ++$i) {
		if (!$a) {
			echo '';
		}
	}
}
$start = microtime(true);
f1();
$stop = microtime(true);
$time1 = $stop - $start;
 
$start = microtime(true);
f2();
$stop = microtime(true);
$time2 = $stop - $start;
 
echo $time1 . "\t";
echo $time2 . "\n";
Test results
comparisontime1time2php_version
799.89%4066375365930155.3.3-7+squeeze1
314.61%6383942646825php-5.3.6-phpbrew_compiled
316.49%6318732631716php-5.3.7-phpbrew_compiled
325.17%6175042625451php-5.3.8-phpbrew_compiled
333.56%6166972673778php-5.3.9-phpbrew_compiled
323.64%6202642627676php-5.3.10-phpbrew_compiled
331.61%6234502690900php-5.3.11-phpbrew_compiled
330.56%6244982688839php-5.3.12-phpbrew_compiled
329.28%6255882685508php-5.3.13-phpbrew_compiled
334.8%6144802671743php-5.3.14-phpbrew_compiled
358.66%6218092852003php-5.3.15-phpbrew_compiled
322.83%6340152680821php-5.3.16-phpbrew_compiled
309.71%6447012641394php-5.3.17-phpbrew_compiled
398.45%6305933143160php-5.3.18-phpbrew_compiled
314.8%6426192665574php-5.3.19-phpbrew_compiled
577.77%3556892410754php-5.4.0-phpbrew_compiled
454.29%4716942614572php-5.4.1-phpbrew_compiled
507.83%4190582547148php-5.4.2-phpbrew_compiled
454.79%4726122621991php-5.4.3-phpbrew_compiled
572.81%3558742394359php-5.4.4-phpbrew_compiled
558.07%3650572402346php-5.4.5-phpbrew_compiled
586.72%3541742432189php-5.4.6-phpbrew_compiled
586.56%3523752419260php-5.4.7-phpbrew_compiled
587.41%3526112423900php-5.4.8-phpbrew_compiled
369.07%6633483111591php-5.4.9-phpbrew_compiled
100%
updown
0%