Ned*_*Ned 6 php performance php-5.5
我读了一些关于PHP引用的文章以及它们如何影响性能.我也看到了一些测试,证明了相同.
大多数这些资源声称其原因是因为引用禁用了写时复制.
但我不明白为什么会导致问题?PHP不会复制数组,例如,因为您通过引用传递它.
function foo(&$var) {
$var->test = 'bar';
xdebug_debug_zval('var');
}
$data = new stdclass;
foo($data);
Run Code Online (Sandbox Code Playgroud)
结果我收到了
(refcount=3, is_ref=1),
object(stdClass)[1]
public 'test' => (refcount=1, is_ref=0),string 'bar' (length=3)
Run Code Online (Sandbox Code Playgroud)
这表明没有复制,并且该函数仍然使用相同的变量(处理实际变量而不是复制).
我错过了什么,为什么会有性能损失?
nic*_*noe -1
我尝试使用 3v4l.com 网站进行基准测试。您可以在这里找到来源: https: //3v4l.org/NqHlZ
以下是我对当前最新 PHP 版本进行 10k 次迭代得到的结果:
Avergages:
- Object through reference, explicitly: 2.5538682937622E-6
- Object through reference, implicitly: 1.8869638442993E-6
- Array through copy-on-write: 1.9102573394775E-6
- Array through reference: 9.3061923980713E-7
Run Code Online (Sandbox Code Playgroud)
Avergages:
- Object through reference, explicitly: 2.2409210205078E-6
- Object through reference, implicitly: 1.7436265945435E-6
- Array through copy-on-write: 1.1391639709473E-6
- Array through reference: 8.7485313415527E-7
Run Code Online (Sandbox Code Playgroud)
Avergages:
- Object through reference, explicitly: 4.6207904815674E-7
- Object through reference, implicitly: 3.687858581543E-7
- Array through copy-on-write: 4.0757656097412E-7
- Array through reference: 3.0455589294434E-7
Run Code Online (Sandbox Code Playgroud)
因此,对于所有最新的 PHP 版本,通过引用传递而不是进行写入时复制不存在性能问题。事实上,“显式地通过引用传递对象”部分实际上不是一种编码方式,因为所有对象都是通过引用传递的,无论是否使用“&”符号。当变量是对象时,也许使用此符号通过引用显式传递变量会导致到达包含数据的另一个内存地址的内存地址,这可以解释为什么它需要更长的时间。
| 归档时间: |
|
| 查看次数: |
103 次 |
| 最近记录: |