Access 1-dimentional array normally vs alias (reference)

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

f2 is slower than f1 by 14.13%

Full source code as executed.
define('LOOP', 2000000);
$a = array();
$a['aa'] = 17;
function f1() {
  global $a;
  for($i=0; $i<LOOP; ++$i) {
    $b = $a['aa'];
    $b = $a['aa'];
    $b = $a['aa'];
    $b = $a['aa'];
    $b = $a['aa'];
  }
}
function f2() {
  global $a;
  for($i=0; $i<LOOP; ++$i) {
    $b = & $a['aa'];
    $b = & $a['aa'];
    $b = & $a['aa'];
    $b = & $a['aa'];
    $b = & $a['aa'];
  }
}
$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
5.78%268253325359275.3.3-7+squeeze1
9.82%381108418544php-5.3.6-phpbrew_compiled
12.45%380963428378php-5.3.7-phpbrew_compiled
13.1%381426431404php-5.3.8-phpbrew_compiled
13.4%377069427599php-5.3.9-phpbrew_compiled
10.45%382555422530php-5.3.10-phpbrew_compiled
11.38%382163425652php-5.3.11-phpbrew_compiled
11.3%381747424885php-5.3.12-phpbrew_compiled
11.47%383166427104php-5.3.13-phpbrew_compiled
11.58%379947423952php-5.3.14-phpbrew_compiled
17.65%382254449709php-5.3.15-phpbrew_compiled
11.28%387131430802php-5.3.16-phpbrew_compiled
11.2%380033422594php-5.3.17-phpbrew_compiled
17.81%411610484912php-5.3.18-phpbrew_compiled
11.94%380068425462php-5.3.19-phpbrew_compiled
20.83%269284325376php-5.4.0-phpbrew_compiled
18.34%269630319069php-5.4.1-phpbrew_compiled
18.69%268835319089php-5.4.2-phpbrew_compiled
18.14%280604331508php-5.4.3-phpbrew_compiled
19.87%266795319817php-5.4.4-phpbrew_compiled
18.86%266460316725php-5.4.5-phpbrew_compiled
19.94%266455319588php-5.4.6-phpbrew_compiled
12.74%285353321711php-5.4.7-phpbrew_compiled
18.7%267084317035php-5.4.8-phpbrew_compiled
18.14%295004348522php-5.4.9-phpbrew_compiled
50%
updown
50%

Comments

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

time1: 32271313 time2: 34744501