php 3幕运算性能优化

我们都知道PHP用写时复制来对变量复制做性能优化, 而在以前的三元式中, 却每次都会复制, 这在操作数是大数组的情况下, 会造成性能问题:


$a=range(1,1000);

$i=0;

$start=microtime(true);

while(++$i<1000){

$b=isset($a)?$a:NULL;

}

var_dump(microtime(true)-$start);

相比, 我们采用if-else来做同样的功能:


$a=range(1,1000);

$i=0;

$start=microtime(true);

while(++$i<1000){

if(isset($a)){

$b=$a;

}else{

$b=NULL;

}

}

var_dump(microtime(true)-$start);


在PHP7 中

第一种 耗时:double(0.010506153106689)

第二种 耗时:double(0.00010108947753906)

第二种比第一种快 100倍

你可能感兴趣的:(php 3幕运算性能优化)