Access array normally vs alias (reference)

Submitted by Hani on Wed, 06/01/2011 - 22:15

f1 is slower than f2 by 5.87%

Full source code as executed.
define('LOOP', 2000000);
 
$a = array();
$a['aa']['bb']['cc'] = 17;
 
function f1() {
  global $a;
  for($i=0; $i<LOOP; ++$i) {
    $b = $a['aa']['bb']['cc'];
  }
}
function f2() {
  global $a;
  for($i=0; $i<LOOP; ++$i) {
    $b = & $a['aa']['bb']['cc'];
  }
}
$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.59%121722012730725.3.3-7+squeeze1
7.82%257935239222php-5.3.6-phpbrew_compiled
7.2%259999242545php-5.3.7-phpbrew_compiled
4.42%262828251692php-5.3.8-phpbrew_compiled
15.98%286397246928php-5.3.9-phpbrew_compiled
4.98%255301243180php-5.3.10-phpbrew_compiled
4.08%255053245044php-5.3.11-phpbrew_compiled
4.3%255832245281php-5.3.12-phpbrew_compiled
4.21%255635245315php-5.3.13-phpbrew_compiled
3.48%257047248397php-5.3.14-phpbrew_compiled
2.13%255453250132php-5.3.15-phpbrew_compiled
2.08%262747257403php-5.3.16-phpbrew_compiled
2.33%255741249919php-5.3.17-phpbrew_compiled
8.44%289145313550php-5.3.18-phpbrew_compiled
4.45%255788244879php-5.3.19-phpbrew_compiled
9.83%179949163838php-5.4.0-phpbrew_compiled
8.35%212296195935php-5.4.1-phpbrew_compiled
8.25%184503170434php-5.4.2-phpbrew_compiled
8.32%217939201207php-5.4.3-phpbrew_compiled
10.91%173227156191php-5.4.4-phpbrew_compiled
8.53%176125162276php-5.4.5-phpbrew_compiled
10.91%172367155417php-5.4.6-phpbrew_compiled
11.61%208273186613php-5.4.7-phpbrew_compiled
6.05%172154162340php-5.4.8-phpbrew_compiled
9.58%202609184897php-5.4.9-phpbrew_compiled
100%
updown
0%

Comments

Submitted by moldfield on Mon, 10/22/2012 - 11:34

time 1: 2052898 time 2: 17512798