从数组中提取变量对性能有影响吗?

发布于 2024-09-02 21:40:45 字数 162 浏览 3 评论 0原文

我发现了 PHP 中很棒的 extract 函数,我认为它真的很方便。然而,我了解到 PHP 中的大多数优点也会影响性能,所以我的问题是,从性能角度来看,使用 extract 会产生哪些影响?

对于大型应用程序来说,从数组中提取变量是禁忌吗?

I have found out about the great extract function in PHP and I think that it is really handy. However I have learn that most things that are nice in PHP also affects performance, so my question is which affect using the extract can have, seen in a performance perspective?

Is it a no-no to use for big applications to extract variables outta arrays?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜点 2024-09-09 21:40:45

更新

正如下面的评论者所指出的,以及多年后我现在敏锐地意识到的 - php 变量写入时复制的。然而,在全局范围内提取防止变量被垃圾收集。正如我之前所说,“考虑你的范围”


取决于数组的大小和提取它的范围,假设你将一个巨大的数组提取到全局命名空间,我可以看到它会产生效果,就像你会看到的那样所有这些数据都在内存中两次 - 我相信它可能会做一些有趣的内部事情,而 PHP 已知会这样做来限制这一点 -

但如果你这样做了,

function bob(){
    extract( array( 'a' => 'woo', 'b' =>'fun', 'c' => 'array' ) );
}

它不会产生真正的持久效果。

长话短说,只需考虑您在做什么、为什么这样做以及范围。

Update:

As the commenter below noted and as I am now keenly aware years later - php variables are copy on write. Extracting in the global scope will however keep the variables from being garbage collected. So as I said before, "consider your scope"


Depends on the size of the array and the scope to which your extracting it, say you extract a huge array to the global namespace, I could see that having an effect, as you will have all that data in memory twice - I believe though it may do some fun internal stuff which PHP is known to do to limit that -

but say you did

function bob(){
    extract( array( 'a' => 'woo', 'b' =>'fun', 'c' => 'array' ) );
}

its going to have no real lasting effect.

Long story short, just consider what you're doing, why your doing it, and the scope.

霊感 2024-09-09 21:40:45

extract 不应用于不受信任的数据。而且它通常对于可信数据没有用(因为已知数组密钥的数量可能有限)。

extract shouldn't be used on untrusted data. And it isn't usually useful for trusted data (because there are likely a limited number of known array keys).

咽泪装欢 2024-09-09 21:40:45

我不明白为什么它会对性能造成如此大的影响,只要您不在大循环中提取巨大的数组即​​可。
但我也从未找到使用 extract 的理由:)

I don't see why it should be such a big performance hit, as long as you don't extract huge arrays in big loops.
But I've never found a reason to use extract either :)

半透明的墙 2024-09-09 21:40:45

在大多数语言中使用数组要好得多,因为编译器和/或解释器可以使用 SIMD 指令与它。

此外,您可能会注意到代码的某些部分是否尝试为数组中的每个值调用一个函数。从性能的角度来看,在打包所有值的情况下仅调用该函数一次会更有效。如果数组太长,多次调用函数的开销将会增加,并且使得检测可能的优化变得更加困难

Working with arrays in most languages is far better because the compiler and/or interpreter can use SIMD instructions with it.

Also you might notice if some part of your code tries to call one function for each value within the array. From a performance point of view it is more efficient to call the function only once with all the values packed. The overhead of calling a function several times will scale up if the array is too long and makes harder to detect possible optimizations

素染倾城色 2024-09-09 21:40:45

我曾经不得不调试一个崩溃的脚本;事实证明 PHP 内存不足。

部分问题在于 extract() 在 25000 个元素的循环中使用。它只能处理大约 2300 个元素,然后就会耗尽内存。我用手动设置变量替换了提取内容(4 个元素的关联数组),循环能够获取大约 5200 条记录。

所以 extract() 肯定存在性能缺陷。

I once had to debug a script which was crashing; it turns out that PHP was running out of memory.

Part of the problem was that extract() was being used in a loop of 25000 elements. It would only get through about 2300 elements before running out of memory. I replaced the extract (associative array of 4 elements) with manually setting the variables, and the loop was able to get through about 5200 records.

So extract() definitely has performance drawbacks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文