霜天部落 | 关注LAMP高性能、高并发架构的设计与研究

php引用能否直接影响到程序的效率

PHP引用的效率的确是高,下面是测试代码,大家跑一下就知道谁更快了:

PHP代码:

< ?php
function getmicrotime()
{
 list($usec, $sec) = explode(" ",microtime());
 return ((float)$usec + (float)$sec);
}

//initialize big array1
$bigArray1 = array();
for($ii=0; $ii<600; $ii++)
{
 $bigArray1[$ii]=$ii.'value';
}

//initialize big array2
$bigArray2 = array();
for($ii=0; $ii<600; $ii++)
{
 $bigArray2[$ii]=$ii.'value';
}

//fuction by reference
function passByRef(&$param)
{
 reset($param);
 while($kek=key($param))
 {
  $param[$key] .= '-processed';
  next($param);
 }
}

//function by copy
function passByCopy($param)
{
 reset($param);
 while($kek=key($param))
 {
  $param[$key] .= '-processed';
  next($param);
 }
}

//Call passByRef 50 times
$startTime = getmicrotime();
for($ii=0; $ii<50; $ii++)
{
 passByRef($bigArray1);
}
$endTime = getmicrotime();
$refTime = $endTime-$startTime;

//Call passByCopy 50 times
$startTime = getmicrotime();
for($ii=0; $ii<50; $ii++)
{
 passByCopy($bigArray2);
}
$endTime = getmicrotime();
$copyTime = $endTime-$startTime;
echo('reference time is '.$refTime.'');
echo('copy time is'.$copyTime.'');
?>