通过引用返回大数据还是作为函数返回?

发布于 2024-11-09 10:41:42 字数 832 浏览 0 评论 0原文

今天在工作中,我与拼贴画就在范围之间传递大数据进行了争论。 误解是,在两个作用域之间传递时,引用使用的内存/CPU 使用量较少。我们构建了一个谁是对的概念证明......所以:

function by_return($dummy=null) {
    $dummy = str_repeat("1",100 * 1024 * 1024);
    return $dummy;
}

function by_reference(&$dummy) {
    $dummy = null;
    $dummy = str_repeat("1",100 * 1024 * 1024);
}
echo memory_get_usage()."/".memory_get_peak_usage()."\n";
//1 always returns: 105493696/105496656
$nagid = by_return();
echo memory_get_usage()."/".memory_get_peak_usage()."\n";
unset($nagid);
//2 always returns:  105493696/210354184 even if we comment 1st part
by_reference($dummy);
echo memory_get_usage()."/".memory_get_peak_usage()."\n";
unset($dummy);

但是根据函数“memory_get_peak_usage()”,通过引用似乎会消耗更多内存

如您所见,使用大数据进行返回比使用作为引用要聪明得多,但是问题是,为什么? 欢迎任何启发:)

On the job today I had an arguement with a collage about passing large data between scopes.
The myth was that reference uses less memory/CPU usage when passing between 2 scopes. We build a proof of concept who was right... so:

function by_return($dummy=null) {
    $dummy = str_repeat("1",100 * 1024 * 1024);
    return $dummy;
}

function by_reference(&$dummy) {
    $dummy = null;
    $dummy = str_repeat("1",100 * 1024 * 1024);
}
echo memory_get_usage()."/".memory_get_peak_usage()."\n";
//1 always returns: 105493696/105496656
$nagid = by_return();
echo memory_get_usage()."/".memory_get_peak_usage()."\n";
unset($nagid);
//2 always returns:  105493696/210354184 even if we comment 1st part
by_reference($dummy);
echo memory_get_usage()."/".memory_get_peak_usage()."\n";
unset($dummy);

But it seems that by reference it consumes more memory according to function "memory_get_peak_usage()"

As you see, using large data for returning is much smarter than using as reference but the question is, why?
Any enlightening is welcomed :)

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

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

发布评论

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

评论(1

池木 2024-11-16 10:41:43

这是由于 php 处理变量的方式造成的,对于使用过 C 或 C++ 的人来说有点违反直觉。

不建议通过引用传递来比 PHP 更智能。 PHP 实际上不会复制数据,除非需要(即当有超过 1 个引用时更改变量的值),这是一种与共享内存页的写时复制非常相似的优化策略。

因此,假设您有一个变量,您在给定脚本中多次按值传递该变量。如果您随后获取该变量并通过引用传递它,那么您实际上是在复制该变量,而不仅仅是获取指向该对象的指针。

这是因为,在内部,PHP zvals(PHP 用于存储变量的数据结构)只能是引用变量或非引用变量。因此,zval 的 ref_count 字段是什么并不重要,因为它不是引用变量(zval 结构的 is_ref 字段)。因此,在内部,PHP 被迫创建一个新的 zval 并将其 is_ref 字段设置为 true,从而使内存加倍。

告诉您的同事不要再试图智取 PHP。除非在整个代码中 100% 完美地完成,否则通过引用传递将导致大量开销并使内存使用量加倍。

有关更详细的讨论,请参阅此链接:http:// /porteightyeight.com/2008/03/18/the-truth-about-php-variables/

This is due to the way php handles variables, and is a bit counter-intuitive to anyone who has worked in C or C++.

Passing by reference to be smarter than PHP isn't advised. PHP doesn't actually make copies of data unless it needs to (i.e. you change a variable's value when there's more than 1 reference to it), an optimization strategy very similar to copy-on-write for shared memory pages.

So, let's say you have a variable that you pass by value several times in a given script. If you then take this variable and pass it by reference, you're actually duplicating the variable rather than just getting a pointer to the object.

This is because internally, PHP zvals (the data structure PHP uses to store variables) can only be reference variables or non-reference variables. So it doesn't matter what the zval's ref_count field is, because it's not a reference variable (the is_ref field of the zval structure). So internally, PHP is forced to create a new zval and set its is_ref field to true, thus doubling the memory.

Tell your co-worker to stop trying to outsmart PHP. Passing by reference unless done 100% perfectly throughout the code will cause a lot of overhead and double the memory usage.

For a more detailed discussion, please see this link: http://porteightyeight.com/2008/03/18/the-truth-about-php-variables/

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