<i id='ITv3v'><tr id='ITv3v'><dt id='ITv3v'><q id='ITv3v'><span id='ITv3v'><b id='ITv3v'><form id='ITv3v'><ins id='ITv3v'></ins><ul id='ITv3v'></ul><sub id='ITv3v'></sub></form><legend id='ITv3v'></legend><bdo id='ITv3v'><pre id='ITv3v'><center id='ITv3v'></center></pre></bdo></b><th id='ITv3v'></th></span></q></dt></tr></i><div id='ITv3v'><tfoot id='ITv3v'></tfoot><dl id='ITv3v'><fieldset id='ITv3v'></fieldset></dl></div>
      • <bdo id='ITv3v'></bdo><ul id='ITv3v'></ul>

    1. <tfoot id='ITv3v'></tfoot>

    2. <legend id='ITv3v'><style id='ITv3v'><dir id='ITv3v'><q id='ITv3v'></q></dir></style></legend>

      <small id='ITv3v'></small><noframes id='ITv3v'>

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

      Why is calling a function (such as strlen, count etc) on a referenced value so slow?(为什么在引用的值上调用函数(例如 strlen、count 等)这么慢?)
      <i id='PxV0c'><tr id='PxV0c'><dt id='PxV0c'><q id='PxV0c'><span id='PxV0c'><b id='PxV0c'><form id='PxV0c'><ins id='PxV0c'></ins><ul id='PxV0c'></ul><sub id='PxV0c'></sub></form><legend id='PxV0c'></legend><bdo id='PxV0c'><pre id='PxV0c'><center id='PxV0c'></center></pre></bdo></b><th id='PxV0c'></th></span></q></dt></tr></i><div id='PxV0c'><tfoot id='PxV0c'></tfoot><dl id='PxV0c'><fieldset id='PxV0c'></fieldset></dl></div>

        <legend id='PxV0c'><style id='PxV0c'><dir id='PxV0c'><q id='PxV0c'></q></dir></style></legend>
          <tbody id='PxV0c'></tbody>

        <small id='PxV0c'></small><noframes id='PxV0c'>

          • <tfoot id='PxV0c'></tfoot>
            • <bdo id='PxV0c'></bdo><ul id='PxV0c'></ul>

                本文介绍了为什么在引用的值上调用函数(例如 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.

                这篇关于为什么在引用的值上调用函数(例如 strlen、count 等)这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <legend id='4oomW'><style id='4oomW'><dir id='4oomW'><q id='4oomW'></q></dir></style></legend>
                        <tbody id='4oomW'></tbody>

                      <small id='4oomW'></small><noframes id='4oomW'>

                    1. <i id='4oomW'><tr id='4oomW'><dt id='4oomW'><q id='4oomW'><span id='4oomW'><b id='4oomW'><form id='4oomW'><ins id='4oomW'></ins><ul id='4oomW'></ul><sub id='4oomW'></sub></form><legend id='4oomW'></legend><bdo id='4oomW'><pre id='4oomW'><center id='4oomW'></center></pre></bdo></b><th id='4oomW'></th></span></q></dt></tr></i><div id='4oomW'><tfoot id='4oomW'></tfoot><dl id='4oomW'><fieldset id='4oomW'></fieldset></dl></div>
                        <bdo id='4oomW'></bdo><ul id='4oomW'></ul>
                        <tfoot id='4oomW'></tfoot>