|
Posted by Chameleon on 12/03/05 19:22
-------
$a = array(............);
$b = $a;
-------
php says that $b points to $a until $b or $a changes.
this means that the code above wants the same time with the code below:
-------
$a = $a = array(............);
$b = & $a;
-------
but when I check this with a script, I found this:
when I use & (ref) the script is faster.
in the code below: (php 5. WinXP. command line script)
first part needs 2.9 secs
second part (in "this line" & exists) needs 3.0 secs
second part (in "this line" & not exist) needs 6.5 secs but not $a nor
$b changes. So I believe that php creates a whole copy of $a in $b.
THE QUESTION:
If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
$v); )
---------THE CODE------------------------------------------
<?
$a =
array(42,345,3245,3245,2345,4564,51346,3457234,13762,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,14,6,45632,46,32,4);
$a['ddd'] = array(345,34,532,45,3245,3,45,2345,7643345,2645612,6423,562);
//------------------first
$t = microtime(true);
for($z = 0; $z < 1000000; $z++) {
foreach($a['ddd'] as $v);
}
$t = microtime(true) - $t;
echo "$t\n";
//------------------second
$t = microtime(true);
for($z = 0; $z < 1000000; $z++) {
$b = & $a['ddd']; // <--------- THIS LINE
foreach($b as $v);
}
$t = microtime(true) - $t;
echo "$t\n";
?>
----------------------------------------------------
Navigation:
[Reply to this message]
|