note 96879 deleted from language.variables.scope by danbrown

From: Date: Sun, 21 Mar 2010 15:03:11 +0000
Subject: note 96879 deleted from language.variables.scope by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167266@lists.php.net to get a copy of this message
Note Submitter: rybadour at gmail dot com ---- Response to moraesdno: Using the superglobal $GLOBALS isn't always faster. In fact, in most situations its slower. And it gets worse the more times you need to use the same variable: <?php //============================ $a=1; function sum() { global $a; $a += 1; $a += 1; } $a=1; function sum2() { $a = $GLOBALS['a']; $a += 1; $a += 1; $GLOBALS['a'] = $a; } ?> Gives a time of: 0.00039410591125488 <-- global $a; 0.00063490867614746 <-- $GLOBALS['a']; <?php //============================ $a=1; function sum() { global $a; $a += 1; $a += 1; } $a=1; function sum2() { $GLOBALS['a'] += 1; $GLOBALS['a'] += 1; } ?> Gives a time of: 0.0003960132598877 <-- global $a; 0.0005040168762207 <-- $GLOBALS['a']; <?php //============================ $a=1; $b=2; function sum() { global $a, $b; $a += $b; $a -= $b; } $a=1; $b=2; function sum2() { $GLOBALS['a'] += $GLOBALS['b']; $GLOBALS['a'] -= $GLOBALS['b']; } ?> Gives a time of: 0.0005180835723877 <-- global $a; 0.00077199935913086 <-- $GLOBALS['a']; <?php //============================ $a=1; $b=2; function sum() { global $a, $b; $a += $b; $a -= $b; } $a=1; $b=2; function sum2() { $a = $GLOBALS['a'] $b = $GLOBALS['b']; $a += $b; $a -= $b; $GLOBALS['a'] = $a; } ?> Gives a time of: 0.00052213668823242 <-- global $a; 0.0013630390167236 <-- $GLOBALS['a'];

« previous php.notes (#167266) next »