note 54823 added to language.variables.scope
| From: | www dot php dot net dot 2nd at mailfilter dot com dot ar | Date: | Sat, 16 Jul 2005 02:39:17 +0000 |
| Subject: | note 54823 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92045@lists.php.net to get a copy of this message | ||
I've been doing some performance tests. I thought I could squeeze some extra cyles using
references, but I discovered they are more mysterious than I imagined (5.0.3).
Consider this:
$a = str_repeat('hola',10000000);
for($n = 0 ; $n < 1000000 ; $n++ ) { $b = $a; unset($b); }
real 0m1.514s
user 0m1.433s
sys 0m0.071s
The above times (as others in this note) are the best out of three attempts in an idle Linux box.
I expected the above to be a bit slow, since constructing $b might imply copying the 40MB string
each time. It was very fast, though. Let's try with references:
$a = str_repeat('hola',10000000);
for($n = 0 ; $n < 1000000 ; $n++ ) { $b =& $a; unset($b); }
real 0m1.488s
user 0m1.380s
sys 0m0.071s
Not much of a gain, but it did took less time to complete. Will this work with functions? Let's
see:
$a = str_repeat('hola',10000000);
for($n = 0 ; $n < 1000000 ; $n++ ) dosome($a);
function dosome($arg){ $t = strlen($arg); }
real 0m3.518s
user 0m3.276s
sys 0m0.088s
Mmm... much slower, but still pretty nice. I didn't use references yet, so let's try them
out:
$a = str_repeat('hola',10000000);
for($n = 0 ; $n < 100 ; $n++ ) dosome($a);
function dosome(&$arg){ $t = strlen($arg); }
real 0m12.071s
user 0m6.190s
sys 0m5.821s
You think it is 3.5 times slower? Think again. It is 350,000 times slower. I had to limit the $n
loop to 100 iterations in order to get those figures! I wonder what happens if I try to access the
variable globally:
$a = str_repeat('hola',10000000);
for($n = 0 ; $n < 1000000 ; $n++ ) dosome();
function dosome(){ $t = strlen($GLOBALS['a']); }
real 0m3.007s
user 0m2.918s
sys 0m0.074s
Notice that using $GLOBALS we're back in bussiness. So using the global keyword should be
exactly the same, isn't it? Wrong again:
$a = str_repeat('hola',10000000);
for($n = 0 ; $n < 100 ; $n++ ) dosome();
function dosome(){ global $a; $t = strlen($a); }
real 0m12.423s
user 0m6.112s
sys 0m5.917s
We're in the '350,000 times slower' domain again. I wonder why the script is spending
so much time in sys.
A couple of additional tests to complete the puzzle:
$a = str_repeat('hola',10000000); $b = Array(&$a);
for($n = 0 ; $n < 100 ; $n++ ) dosome();
function dosome(){ $t = strlen($GLOBALS['b'][0]); }
real 0m12.087s
user 0m6.068s
sys 0m5.955s
$a = str_repeat('hola',10000000); $b = Array(&$a);
for($n = 0 ; $n < 100 ; $n++ ) dosome();
function dosome(){ global $b; $t = strlen($b[0]); }
real 0m12.158s
user 0m6.023s
sys 0m5.971s
I guess the $GLOBALS trick doesn't help when we access a reference stored in the global
variable.
I'm completely confused, now. At this light, I will review my usage of the global keyword as
well as for the references. I hope someone can benefit from this study too.
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54823
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54823&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54823&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54823&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54823&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54823&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54823&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54823&report=yes
Search -- http://master.php.net/manage/user-notes.php