速度:参考还是按价值? [英] speed: by reference or by value?

查看:80
本文介绍了速度:参考还是按价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-------

$ a =数组(............);

$ b = $ a ;

-------

php说$ b指向$ a直到$ b或$ a更改。

this意味着上面的代码需要与下面的代码同时:

-------

$ a = $ a =数组(..... .......);

$ b =& $ a;

-------


但是当我用脚本检查时,我发现了这个:

当我使用& (参考)脚本更快。


代码如下:(php 5. WinXP。命令行脚本)

第一部分需要2.9秒

第二部分(在此行和存在中)需要3.0秒

第二部分(在此行和不存在)需要6.5秒而不是$ a或

$ b更改。所以我相信php会在$ b中创建$ a的完整副本。


问题:

如果我想要一个FAST脚本,我必须使用& ;到处? (例如foreach($ b as&

$ v);)


---------代码----- -------------------------------------

<?

$ a =

array(42,345,3245,3245,2345,4564,51346,3457234,137 62,4561,3465,23,451,345,32,45,23,45, 23,4,76,3,45,1 4,6,45632,46,32,4);


$ a [''ddd''] =数组(345, 34,532,45,3245,3,45,2345,7643345,2645612,6423,562);


// --------------- ---首先

$ t = microtime(true);

for($ z = 0; $ z< 1000000; $ z ++){

foreach($ a [''ddd'']为$ v);

}

$ t = microtime(true) - $ t;

echo" $ t\\\
";

// ------------------ second

$ t = microtime(true);

for($ z = 0; $ z< 1000000; $ z ++){

$ b =& $ A [ DDD]; //< ---------这条线

foreach($ b as $ v);

}

$ t = microtime(true) - $ t;

echo" $ t\\\
" ;;

?>

--- -------------------------------------------------

-------
$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,137 62,4561,3465,23,451,345,32,45,23,45,23,4,76,3,45,1 4,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";
?>
----------------------------------------------------

推荐答案

a = array(............);
a = array(............);


b =


a;

-------

php说
a;
-------
php says that


这篇关于速度:参考还是按价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆