免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP技巧

引用的解释

时间:2011-05-16 15:55:22  来源:站内  作者:潘春会

   for($n=0; $n<$size; $n++) {
       $x .= $arr[$n];
   }
   return $x;
}

function test_val($arr) {
   $size = sizeof($arr);
   for($n=0; $n<$size; $n++) {
       $x .= $arr[$n];
   }
   return $x;
}

// fill array
for($n=0; $n<500000; $n++) {
   $ar[] = "test".$n;
}
$Bs = array_sum(explode(' ', microtime()));
test_ref($ar);
echo "<br />The function using a reference took ".(array_sum(explode(' ', microtime()))-$Bs);
$Bs = array_sum(explode(' ', microtime()));
test_val($ar);
echo "<br />The funktion using a value took: ".(array_sum(explode(' ', microtime()))-$Bs);

The function using a reference took 10.035583019257
The funktion using a value took: 10.531390190125

Ed (23-Jan-2006 03:29)

 

 

 

Responding to Slava Kudinov.  The only reason why your script takes longer when you pass by reference is that you do not at all modify the array that your passing to your functions.  If you do that the diffrences in execution time will be a lot smaller.  In fact  passing by reference will be faster if just by a little bit.

Slava Kudinov <skudinov gmail com> (22-Jan-2006 06:01)

 

 

 

In addition to the "array vs &array" example below, php 5.1.2. It seems passing an array by reference slower than by value, consider:
<?php
function test_ref(&$arr) {
  
$size = sizeof($arr);
   for(
$n=0; $n<$size; $n++) {
      
$x = 1;
   }
}

function
test_val($arr) {
  
$size = sizeof($arr);
   for(
$n=0; $n<$size; $n++) {
      
$x = 1;
   }
}

// fill array
for($n=0; $n<2000; $n++) {
  
$ar[] = "test".$n;
}
$time = microtime();
test_ref($ar);
echo
"<br />The function using a reference took "
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门