Use empty function or direct comparison (with defined variable)

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

f1 is slower than f2 by 0.84%

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',10000000);
 
function f1() {
	for($a = '', $i=0; $i<LOOP; ++$i) {
		if (empty($a)) {
			echo '';
		}
	}
}
function f2() {
	for($a = '', $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
4.95%410517543083865.3.3-7+squeeze1
5.01%613530644243php-5.3.6-phpbrew_compiled
0.68%655736651307php-5.3.7-phpbrew_compiled
0.83%625249630410php-5.3.8-phpbrew_compiled
1.48%628202619011php-5.3.9-phpbrew_compiled
0.85%627954622633php-5.3.10-phpbrew_compiled
0.59%645617641837php-5.3.11-phpbrew_compiled
0.08%640844641370php-5.3.12-phpbrew_compiled
0.38%644105641664php-5.3.13-phpbrew_compiled
26.83%796795628250php-5.3.14-phpbrew_compiled
4.33%633050660456php-5.3.15-phpbrew_compiled
2.31%640425655188php-5.3.16-phpbrew_compiled
4.77%648924619401php-5.3.17-phpbrew_compiled
0.99%638195631944php-5.3.18-phpbrew_compiled
0.52%631162627925php-5.3.19-phpbrew_compiled
0.7%368012370578php-5.4.0-phpbrew_compiled
1.39%506881499933php-5.4.1-phpbrew_compiled
0.64%431447434215php-5.4.2-phpbrew_compiled
1.76%528531519398php-5.4.3-phpbrew_compiled
0.13%365144365613php-5.4.4-phpbrew_compiled
0.33%364542363336php-5.4.5-phpbrew_compiled
0.38%366266364877php-5.4.6-phpbrew_compiled
1.57%365432371174php-5.4.7-phpbrew_compiled
1.04%364276368057php-5.4.8-phpbrew_compiled
1.71%655530644500php-5.4.9-phpbrew_compiled
50%
updown
50%