I thought that if the $v
don t change [foreach($a as $v)
], the real copy will not happen because of copy on write, but why it is fast when pass by reference?
影响不在于<代码>v,而在于$a
,即巨大的阵容。 您要么将它作为价值,要么作为职能参考。 在职能部分,它的价值(测试1)或参照(测试2)。
你有两部法典(第1条和第2条)。
<><><>> 采用<代码>foreach。 页: 1 当贵重时,则在价值copy上填满。 如果你重复提及,就没有复制件。
由于你在测试2中使用这一提法,其速度更快。 这些价值观无需复制。 但是,在试验1中,你通过阵列作为价值,阵列被复制。
<><>><>>>> 采用<条码>。 这里实际上没有任何东西。 在这两种情况下。 你们能够从阵列中获得变数和读值。 即便有参考书或复制件(参见copy on letter Bestation in PHP),这还是完全一样。
您现在可能想知道,为什么有is的代码差异。 2. 差异不是因为<条码>至条码>,而是因为<条码>。 请注意<代码>。 PHP内部制作一份复印件,因为它需要一份复印件,而不是参考。
阅读: rel=“noreferer”>。 Do not use PHP reference by Johannes Schlüter
我还汇编了一套测试。 但我更具体地将守则纳入测试职能。
- Blank - What s the difference in calling the function?
- Count - Does
count
make a difference?
- For - What happens with
for
only (not count
)?
- Foreach - Just
foreach
- even breaking on first element.
每一次测试均分两种版本,一个称为_copy
。 (将阵列作为复制件并入功能)和一份称为<代码>_ref (参照阵列)。
并不总是这些缩微标语告诉你真相,但如果你能够孤立具体要点,你就可以很好地做受过教育的猜测,例如,没有<<<<>for>但count
。 影响:
function blank_copy($a){
}
function blank_ref(&$a){
}
function foreach_copy($a){
foreach($a as $v) break;
}
function foreach_ref(&$a){
foreach($a as $v) break;
}
function count_copy($a){
$cnt = count($a);
}
function count_ref(&$a){
$cnt = count($a);
}
function for_copy($a){
for($i=0;$i<100000;$i++)
$a[$i];
}
function for_ref(&$a){
for($i=0;$i<100000;$i++)
$a[$i];
}
$tests = array( blank_copy , blank_ref , foreach_copy , foreach_ref , count_copy , count_ref , for_copy , for_ref );
$x = array_fill(0, 100000, xxxxx );
$count = count($x);
$runs = 10;
ob_start();
for($i=0;$i<10;$i++)
{
shuffle($tests);
foreach($tests as $test)
{
$begin = microtime(true);
for($r=0;$r<$runs;$r++)
$test($x);
$end = microtime(true);
$result = $end - $begin;
printf("* % .-16s: %f
", $test, $result);
}
}
$buffer = explode("
", ob_get_clean());
sort($buffer);
echo implode("
", $buffer);
产出:
* blank_copy......: 0.000011
* blank_copy......: 0.000011
* blank_copy......: 0.000012
* blank_copy......: 0.000012
* blank_copy......: 0.000012
* blank_copy......: 0.000015
* blank_copy......: 0.000015
* blank_copy......: 0.000015
* blank_copy......: 0.000015
* blank_copy......: 0.000020
* blank_ref.......: 0.000012
* blank_ref.......: 0.000012
* blank_ref.......: 0.000014
* blank_ref.......: 0.000014
* blank_ref.......: 0.000014
* blank_ref.......: 0.000014
* blank_ref.......: 0.000015
* blank_ref.......: 0.000015
* blank_ref.......: 0.000015
* blank_ref.......: 0.000015
* count_copy......: 0.000020
* count_copy......: 0.000022
* count_copy......: 0.000022
* count_copy......: 0.000023
* count_copy......: 0.000024
* count_copy......: 0.000025
* count_copy......: 0.000025
* count_copy......: 0.000025
* count_copy......: 0.000026
* count_copy......: 0.000031
* count_ref.......: 0.113634
* count_ref.......: 0.114165
* count_ref.......: 0.114390
* count_ref.......: 0.114878
* count_ref.......: 0.114923
* count_ref.......: 0.115106
* count_ref.......: 0.116698
* count_ref.......: 0.118077
* count_ref.......: 0.118197
* count_ref.......: 0.123201
* for_copy........: 0.190837
* for_copy........: 0.191883
* for_copy........: 0.193080
* for_copy........: 0.194947
* for_copy........: 0.195045
* for_copy........: 0.195944
* for_copy........: 0.198314
* for_copy........: 0.198878
* for_copy........: 0.200016
* for_copy........: 0.227953
* for_ref.........: 0.191918
* for_ref.........: 0.194227
* for_ref.........: 0.195952
* for_ref.........: 0.196045
* for_ref.........: 0.197392
* for_ref.........: 0.197730
* for_ref.........: 0.201936
* for_ref.........: 0.207102
* for_ref.........: 0.208017
* for_ref.........: 0.217156
* foreach_copy....: 0.111968
* foreach_copy....: 0.113224
* foreach_copy....: 0.113574
* foreach_copy....: 0.113575
* foreach_copy....: 0.113879
* foreach_copy....: 0.113959
* foreach_copy....: 0.114194
* foreach_copy....: 0.114450
* foreach_copy....: 0.114610
* foreach_copy....: 0.118020
* foreach_ref.....: 0.000015
* foreach_ref.....: 0.000016
* foreach_ref.....: 0.000016
* foreach_ref.....: 0.000016
* foreach_ref.....: 0.000018
* foreach_ref.....: 0.000019
* foreach_ref.....: 0.000019
* foreach_ref.....: 0.000019
* foreach_ref.....: 0.000019
* foreach_ref.....: 0.000020