unset和= null之间的区别 [英] the difference between unset and = null

查看:157
本文介绍了unset和= null之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自随机php.net帖子:

如果您正在执行$ whatever = null;然后您要重写变量的 数据.您可能会释放/缩小内存,但可能会窃取CPU 从真正需要它们的代码中循环过来,从而导致 总体执行时间更长.

If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

显然这是没有争议的真理,所以也许有人会乐于解释.

Apparently this is the undisputed truth so maybe someone would be so kind as to explain.

我的意思是,unset是不是神奇地不执行任何汇编指令,而$whatever = null;是吗?给出的答案和说一样有用

I mean, what, does unset magically not execute any assembly instructions whereas $whatever = null; does? The answer, as given, is about as useful as saying

$ whatever = null会重置缓冲区和L1缓存,而unset会清除缓冲区并重置L2缓存.

$whatever = null resets the buffer and the L1 cache whereas unset clears the buffer and resets the L2 cache.

Techno Mumbo jumbo不能构成答案.

Techno mumbo jumbo doesn't constitute an answer.

推荐答案

这两种方法之间的重要区别是unset($a)还将符号表中的$a删除了.例如:

An important difference between both methods is that unset($a) also removes $a from the symbol table; for example:

$a = str_repeat('hello world ', 100);
unset($a);
var_dump($a);

输出:

Notice: Undefined variable: a in xxx
NULL

但是使用$a = null时:

$a = str_repeat('hello world ', 100);
$a = null;
var_dump($a);

输出:

NULL

我也通过基准测试运行了代码,发现$a = null比其unset()快大约6%.似乎更新符号表条目比删除符号表条目要快.

I ran the code through a benchmark as well and found that $a = null is roughly 6% faster than its unset() counterpart. It seems that updating a symbol table entry is faster than removing it.

附录

另一个区别(如该小脚本所示)似乎是每次调用后恢复多少内存:

The other difference (as seen in this small script) seems to be how much memory is restored after each call:

echo memory_get_usage(), PHP_EOL;
$a = str_repeat('hello world ', 100);
echo memory_get_usage(), PHP_EOL;
// EITHER unset($a); OR $a = null;
echo memory_get_usage(), PHP_EOL;

使用unset()时,将返回除64字节以外的所有内存,而$a = null;释放除272字节以外的所有内存.我没有足够的知识来知道为什么这两种方法之间有208个字节的差异,但这仍然是一个差异.

When using unset() all but 64 bytes of memory are given back, whereas $a = null; frees all but 272 bytes of memory. I don't have enough knowledge to know why there's a 208 bytes difference between both methods, but it's a difference nonetheless.

这篇关于unset和= null之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆