[TYPO3-core] Wrapper methods around globals?

Jigal van Hemert jigal.van.hemert at typo3.org
Wed Mar 19 20:54:39 CET 2014


Hi,

On 19-3-2014 11:42, François Suter wrote:
> In a patch I submitted this morning [1], I added a wrapper method around
> $GLOBALS['BE_USER'], because it makes it much easier to work with such
> globals in an IDE, having type hinting and code completion. I did it
> because I saw other places in the Core using this way of doing this.
>
> Now Kay Strobach thinks this should not be done for performance reasons.
> Do we have guidelines about this?

Just did a performance check and the wrapper function is indeed 
relatively a lot slower (approx. 2.5 times slower).

The numbers:
Direct access
0.20600008964539 -> 0.00020600008964539 ms;
Wrapper function
0.51699995994568 -> 0.00051699995994568 ms;

So instead of 2 microseconds we use 5 microseconds per call.
Compared to the advantages of autocomplete, code analysis, mocking in 
unit tests, etcetera this is negligible IMO.

<?php

$plaap = new stdClass();
$plaap->a = array('a' => 'blurp');
$iterations = 1000000;
$i = 0;

$start = microtime(true);
for($i = 0; $i < $iterations; $i++) {
	$localVar = $plaap->a['a'];
}
$middle = microtime(true);
for($i = 0; $i < $iterations; $i++) {
	$localVar = getGlobal()->a['a'];
}
$end = microtime(true);

echo ($middle - $start) . ' -> ' . ($middle - $start)*1000/$iterations . 
' ms; ' . chr(10);
echo ($end - $middle) . ' -> ' . ($end - $middle)*1000/$iterations . ' 
ms; ' . chr(10);

function getGlobal() {
	return $GLOBALS['plaap'];
}

?>

-- 
Jigal van Hemert
TYPO3 CMS Active Contributor

TYPO3 .... inspiring people to share!
Get involved: typo3.org


More information about the TYPO3-team-core mailing list