Closed oricgn closed 9 years ago
Why? This will create a reference which PHP is very costly. Especially for large arrays.
Please have a look at https://github.com/Phorum/Core/issues/598
Heh, well, that was an old ticket of mine. I would like to see that benchmarked again. I have recently been removing references in favor of copies in other code o work on to improve performance.
PHP version 5.4.41-0+deb7u1
A1() 1.299 ms B1() 3.068 ms
A2() 1.617 ms B2() 3.785 ms
<?php
echo 'PHP version '.phpversion().'<br><br>';
$PHORUM = array('A' => 42, 'B' => 'abcdefgh', 'C' => array('C1' >= 43, 'C2' => 'ijklmnop'));
function A1(){ global $PHORUM; $PHORUM['A'] = 42; }
function A2(){ global $PHORUM; $PHORUM['A'] = 42; $PHORUM['B'] = 'xxx'; }
function B1(){ $PHORUM = $GLOBALS['PHORUM']; $PHORUM['A'] = 42; }
function B2(){ $PHORUM = $GLOBALS['PHORUM']; $PHORUM['A'] = 42; $PHORUM['B'] = 'xxx'; }
$time_start = microtime(true);
for ($i=0; $i < 10000000; ++$i) { A1(); }
$time_end = microtime(true);
echo 'A1() '.round($time_end - $time_start, 3).' ms<br>';
$time_start = microtime(true);
for ($i=0; $i < 10000000; ++$i) { B1(); }
$time_end = microtime(true);
echo 'B1() '.round($time_end - $time_start, 3).' ms<br><br>';
$time_start = microtime(true);
for ($i=0; $i < 10000000; ++$i) { A2(); }
$time_end = microtime(true);
echo 'A2() '.round($time_end - $time_start, 3).' ms<br>';
$time_start = microtime(true);
for ($i=0; $i < 10000000; ++$i) { B2(); }
$time_end = microtime(true);
echo 'B2() '.round($time_end - $time_start, 3).' ms';
?>
This is a better script since the $PHORUM structure can be quite large and it has objects in it. http://3v4l.org/9d9mu
The end result is even worse if anything. The anonymous function performance is interesting. And frankly, it look like PHP 7 is going to make all of this a moot point no matter what.
PHP 7 is still far away and even in PHP 7 the global command is still a little bit faster. So I think the conversion from $PHORUM = $GLOBALS['PHORUM'] to global $PHORUM is reasonable.
Convert $PHORUM = $GLOBALS['PHORUM'] to global $PHORUM