<code>$array = range(1, 10000);
function test1($array)
{
return $array;
}
function &test2($array)
{
return $array;
}
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
$arr = test1($array);
$arr[$i] = 'new';
}
$end = microtime(true);
echo "Cost ".($end - $start)."\n";
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
$arr = test2($array);
$arr[$i] = 'new';
}
$end = microtime(true);
echo "Cost ".($end - $start)."\n";
</code>输出结果:
cost 5.5163149833679
cost 6.3323628902435
虽然差距不大
<code>$array = range(1, 10000);
function test1($array)
{
return $array;
}
function &test2($array)
{
return $array;
}
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
$arr = test1($array);
$arr[$i] = 'new';
}
$end = microtime(true);
echo "Cost ".($end - $start)."\n";
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
$arr = test2($array);
$arr[$i] = 'new';
}
$end = microtime(true);
echo "Cost ".($end - $start)."\n";
</code>输出结果:
cost 5.5163149833679
cost 6.3323628902435
虽然差距不大
拷贝需要开辟空间初始化之类的,相当于创建几个新的对象。
而引用只是一个指针。
首先、根据 PHP: 引用返回 - Manual 的解释,以上两种写法并没有什么区别,你可以理解没有加 & 的那个函数底层引擎是会优化的,所以可能确实快了一点点,所以手册上也是不推荐的。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号