note 95126 added to language.variables.scope
| From: | merlinyoda at dotproject dot net | Date: | Mon, 14 Dec 2009 21:32:32 +0000 |
| Subject: | note 95126 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163962@lists.php.net to get a copy of this message | ||
In response to moraesdno at gmail dot com
It would seem offhand that using the $GLOBALS array is *slightly* faster but, unless you're
calling that function millions of times, you're probably not going to see any noticeable
benefit in using one over the other ... especially when weighed against other concerns such as code
maintainability.
Here's the code I used for testing (based off code test in previous post):
<?php
define ('NUM_RUNS', 1);
define ('NUM_CALLS', 1000);
//Using the keyword global
function sum1() {
global $a, $b;
$a += $b;
}
//Using the superglobal array
function sum2() {
$GLOBALS['a'] += $GLOBALS['b'];
}
$func_array = array(0 => 'sum2', 1 => 'sum1');
$time_sum = array();
for ($k = 0; $k < NUM_RUNS; $k++){
for ($j = 0; $j < count($func_array) ; $j++) {
$a = 1;
$b = 2;
$t = microtime(true);
for($i=0; $i<NUM_CALLS; $i++) {
$func_array[$j]();
}
$total_time = microtime(true) - $t;
$time_sum[$j] += $total_time;
// echo $func_array[$j] . '() : '. $total_time . ' -- a = ' . $a .
"\n";
}
}
echo ' NUM_CALLS = ' . NUM_CALLS . "\n";
echo ' NUM_RUNS = ' . NUM_RUNS . "\n";
for ($j = 0; $j < count($func_array) ; $j++) {
echo $func_array[$j] . '() total = ' . $time_sum[$j];
echo ' average = ' . ($time_sum[$j] / NUM_RUNS) . "\n";
}
?>
and the following are the results after several runs changing both the order the functions were
called in (as it seemed to make a difference) and as well as the number of calls.
./global-test.php
NUM_CALLS = 1000
NUM_RUNS = 1
sum1() total = 0.00862908363342 average = 0.00862908363342
sum2() total = 0.00778603553772 average = 0.00778603553772
./global-test.php
NUM_CALLS = 1000
NUM_RUNS = 1
sum2() total = 0.00794410705566 average = 0.00794410705566
sum1() total = 0.00887393951416 average = 0.00887393951416
./global-test.php
NUM_CALLS = 1000
NUM_RUNS = 50
sum1() total = 0.413125038147 average = 0.00826250076294
sum2() total = 0.379062414169 average = 0.00758124828339
./global-test.php
NUM_CALLS = 1000
NUM_RUNS = 50
sum2() total = 0.385180234909 average = 0.00770360469818
sum1() total = 0.415044784546 average = 0.00830089569092
./global-test.php
NUM_CALLS = 10000
NUM_RUNS = 100
sum1() total = 8.20324921608 average = 0.0820324921608
sum2() total = 7.44592571259 average = 0.0744592571259
./global-test.php
NUM_CALLS = 10000
NUM_RUNS = 100
sum2() total = 7.57914853096 average = 0.0757914853096
sum1() total = 8.00758075714 average = 0.0800758075714
----
Server IP: 69.147.83.197
Probable Submitter: 65.117.116.130
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/95126
Del: integrated -- https://master.php.net/note/delete/95126/integrated
Del: useless -- https://master.php.net/note/delete/95126/useless
Del: bad code -- https://master.php.net/note/delete/95126/bad+code
Del: spam -- https://master.php.net/note/delete/95126/spam
Del: non-english -- https://master.php.net/note/delete/95126/non-english
Del: in docs -- https://master.php.net/note/delete/95126/in+docs
Del: other reasons-- https://master.php.net/note/delete/95126
Reject -- https://master.php.net/note/reject/95126
Search -- https://master.php.net/manage/user-notes.php