为什么在引用的值上调用函数(例如 strlen、count 等)这么慢?

我刚刚在 PHP 中发现了一些非常奇怪的东西.

I've just found something very strange in PHP.

如果我通过引用将一个变量传递给一个函数,然后在它上面调用一个函数,它难以置信地慢.

If I pass in a variable to a function by reference, and then call a function on it, it's incredibly slow.

如果循环内部函数调用并且变量很大,它可能比按值传递变量慢许多数量级.

If you loop over the inner function call and the variable is large it can be many orders of magnitude slower than if the variable is passed by value.

示例:

<?php
function TestCount(&$aArray)
{
    $aArray = range(0, 100000);
    $fStartTime = microtime(true);

    for ($iIter = 0; $iIter < 1000; $iIter++)
    {
        $iCount = count($aArray);
    }

    $fTaken = microtime(true) - $fStartTime;

    print "took $fTaken seconds
";
}

$aArray = array();
TestCount($aArray);
?>

在我的机器上运行这始终需要大约 20 秒(在 PHP 5.3 上).

This consistently takes about 20 seconds to run on my machine (on PHP 5.3).

但如果我将函数更改为按值传递(即 function TestCount($aArray) 而不是 function TestCount(&$aArray)),则它会运行在大约 2 毫秒内 - 实际上快了 10,000 倍!

But if I change the function to pass by value (ie function TestCount($aArray) instead of function TestCount(&$aArray)), then it runs in about 2ms - literally 10,000 times faster!

对于其他内置函数(例如 strlen)和用户定义函数也是如此.

The same is true for other built-in functions such as strlen, and for user-defined functions.

怎么回事?

推荐答案

我发现 2005 年的错误报告正是描述了这个问题:http://bugs.php.net/bug.php?id=34540

I found a bug report from 2005 that describes exactly this issue: http://bugs.php.net/bug.php?id=34540

所以问题似乎在于,当将引用的值传递给不接受引用的函数时,PHP 需要复制它.

So the problem seems to be that when passing a referenced value to a function that doesn't accept a reference, PHP needs to copy it.

这可以用这个测试代码来证明:

This can be demonstrated with this test code:

<?php
function CalledFunc(&$aData)
{
    // Do nothing
}

function TestFunc(&$aArray)
{
    $aArray = range(0, 100000);
    $fStartTime = microtime(true);

    for ($iIter = 0; $iIter < 1000; $iIter++)
    {
        CalledFunc($aArray);
    }

    $fTaken = microtime(true) - $fStartTime;

    print "took $fTaken seconds
";
}

$aArray = array();
TestFunc($sData);
?>

这运行得很快,但是如果您将 function CalledFunc(&$aData) 更改为 function CalledFunc($aData),您将看到类似的减速count 示例.

This runs quickly, but if you change function CalledFunc(&$aData) to function CalledFunc($aData) you'll see a similar slow-down to the count example.

这很令人担忧,因为我编写 PHP 代码已经有一段时间了,但我对这个问题一无所知.

This is rather worrying, since I've been coding PHP for quite a while and I had no idea about this issue.

幸运的是,有一个适用于许多情况的简单解决方法 - 在循环内使用临时局部变量,并在最后复制到引用变量.

Fortunately there's a simple workaround that is applicable in many cases - use a temporary local variable inside the loop, and copy to the reference variable at the end.

相关文章