initialized vs uninitialized variable access

Submitted by tomek on Sun, 05/08/2011 - 13:47

f2 is slower than f1 by 188.34% (1.9 times slower)

Full source code as executed.
define('LOOP',10000000);
 
error_reporting(E_ERROR);
 
function initialized() {
  $a = '';
  $a .= 'test';
}
 
function uninitialized() {
  $a .= 'test';
}
 
function f1() {
  for($i=0; $i<LOOP; ++$i) {
    initialized();  
  }
}
 
function f2() {
  for($i=0; $i<LOOP; ++$i) {
    uninitialized();
  }
}
 
$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
110.03%11544956242477645.3.5-1ubuntu7.2
94.86%12577717245093665.3.3-7
153.81%19587837497165305.3.5-1ubuntu7.2
96.81%12389765243843125.3.3-7+squeeze1
203.5%15143724596107php-5.3.6-phpbrew_compiled
170.73%14913074037429php-5.3.7-phpbrew_compiled
167.26%14768583947054php-5.3.8-phpbrew_compiled
172.45%14943624071387php-5.3.9-phpbrew_compiled
148.53%16533084108938php-5.3.10-phpbrew_compiled
180.01%14911254175294php-5.3.11-phpbrew_compiled
180.25%14900724175864php-5.3.12-phpbrew_compiled
181.3%14901384191746php-5.3.13-phpbrew_compiled
178.5%14776174115204php-5.3.14-phpbrew_compiled
190.35%15099354384114php-5.3.15-phpbrew_compiled
185.57%14963574273187php-5.3.16-phpbrew_compiled
182.61%15058454255597php-5.3.17-phpbrew_compiled
217.3%14738344676485php-5.3.18-phpbrew_compiled
168.92%15002204034453php-5.3.19-phpbrew_compiled
239.88%10951923722314php-5.4.0-phpbrew_compiled
206.47%13216344050462php-5.4.1-phpbrew_compiled
222.55%12205153936794php-5.4.2-phpbrew_compiled
208.13%13252264083441php-5.4.3-phpbrew_compiled
246.95%11089493847505php-5.4.4-phpbrew_compiled
248.4%11045853848346php-5.4.5-phpbrew_compiled
242.93%11374813900791php-5.4.6-phpbrew_compiled
197.52%12659523766423php-5.4.7-phpbrew_compiled
253.37%11115493927912php-5.4.8-phpbrew_compiled
224.54%12738624134157php-5.4.9-phpbrew_compiled
50%
updown
50%