Oct 17

debug_backtrace 的性能测试 不指定

bkkkd , 22:59 , 开发应用 , 评论(1) , 引用(0) , 阅读(9906) , Via 本站原创 | |
我最近在写一个语言更换的程序.准备把语言包写到一个数组里.然后再从里面调出来.
如:

$lang=array(
                'Username'=>array(
                                          'en'=>'Username',
                                          'cn'=>'用户名',
                                          )
                );
$db_lang='cn';
function __($language)
{
       global $lang,$db_lang;
       return $lang[$language][$db_lang];
}

但为了效率,不想把所有语言包都都引用到程序里.因为很多语言包都是只在一个程序里面才用到.所以我需要区分不同的的各种语言包的程序.所以我想用运行__()代码的文件地址作为区分的不同的语言包这样每次只要引用很小部分的内容可以得到我们需要的语言了.
所以我想用debug_backtrace()来得到这个文件地址.我已经写好了相关的程序.但我想到一个问题,效率.这个函数本来是用于测试bug的时候用的.所以他不一定有很好的效率.其于这个的心态我对debug_backtrace()进行了测试

<?
/*
*  程序名称:测试debug_backtrace程序的性能
*  文 件 名:
*  作    者:bkkkd
*  作者博客:http://atim.cn
*  开始时间:
*  结束时间:
*  作    用:
*  文件地址:
*  数据结构:
*/
$count=empty($_GET['count'])?1:$_GET['count'];
$reset=0;
$c=array();
$ic=empty($_GET['ic'])?1:$_GET['ic'];
If($reset==1)
{
  $wdb="<?php\n";
  For($i=0; $i<$count; $i++)
  {
    $wdb.="debug();";
  }
  $wdb.="\n?>";
  For($i=0; $i<1000; $i++)
  {
    writeover ("./test_$count/".$i.".php",$wdb);
  }
  If($count==1000)
  {
    exit;
  }
  $next_count=getNextNum($count);
  $URL="?count=$next_count";
  echo("<meta http-equiv='refresh' content='2;url=$URL'>");
}
else
{
  RunTime_Begin();
  For($i=0; $i<$ic; $i++)
  {
    Include "./test_$count/".$i.".php";
  }
  $runtime=RunTime_End();
  echo($runtime);
  writeover("./rs.cvs","$count\t$ic\t$runtime\n","ab+");
  
  If($ic==1000&&$count==1000)
  {
    exit;
  }
  elseif($ic==1000)
  {
    $next_ic=1;
    $next_count=getNextNum($count);
  }
  Else
  {
      $next_ic=getNextNum($ic);
    $next_count=$count;
  }
  $URL="?ic=$next_ic&count=$next_count";
  echo("<meta http-equiv='refresh' content='2;url=$URL'>");
}
/**
* function description:
* @Date:
* @param    (type)parameter  :description
**/
Function getNextNum($num)
{
  $next=1;
  Switch($num)
  {
    Case 1:$next=50;break;
    Case 50:$next=100;break;
    Case 100:$next=500;break;
    Case 500:$next=1000;break;
  }
  Return $next;
}
/**
* function description:
* @Date:
* @param    (type)parameter  :description
**/
Function debug()
{
  global $c;
  $r=debug_backtrace();
  $c[]=$r[0]['file'];
}
function RunTime_Begin()
{
  global $_starttime;
  $_nowtime = explode(" ", microtime());
  $_starttime = $_nowtime[1] + $_nowtime[0];
}

function RunTime_End()
{
  global $_starttime;
  $_nowtime = explode(" ", microtime());
  $_endtime = $_nowtime[1] + $_nowtime[0];
  $_totaltime = $_endtime - $_starttime;
  return $_totaltime;
}
function writeover($path,$data,$opentype='rb+',$lock=1)
{
  touch($path);
  $fp=fopen($path,$opentype);
  $lock==1?flock($fp,LOCK_EX):flock($fp,LOCK_SH);
  fwrite($fp,$data);
  if($opentype=="rb+") ftruncate($fp,strlen($data));
  fclose($fp);
}
?>

第一个数据为一个文件内有多少个debug_backtrace()
第二个数据为调用了多少个文件
第三个数据为调用所需要的运行时间

1  1  0.000504016876221
1  50  0.456976890564
1  100  0.493342876434
1  500  3.67333292961
1  1000  5.43402886391
50  1  0.012130022049
50  50  0.517045974731
50  100  0.655413866043
50  500  4.47166490555
50  1000  6.46373391151
100  1  0.0187499523163
100  50  0.537144899368
100  100  0.711118936539
100  500  4.77257990837
100  1000  7.16184091568
500  1  0.0180358886719
500  50  0.777477025986
500  100  1.17954492569
500  500  7.0535929203
500  1000  11.6655769348
1000  1  0.0255968570709
1000  50  1.3075799942
1000  100  1.73514294624
1000  500  9.95028400421
1000  1000  17.112336874

从上面的数据来看.这个debug_backtrace()不适合多次使用.
所以我决定采用另一个方案.以后再写我的方案.
lee Homepage
October 18, 2008 13:02
[emot]grin[/emot]看不懂.php忘记了
bkkkd 回复于 October 20, 2008 09:35
其实编程都是一样的。差不多
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]