如何计算PHP页面执行时间?

zujun314 2005-10-25 11:15:31
我在比较各种数据库查询效率时,想采用页面执行时间多少的方式来测试数据库的效率,但是一时不知道怎么写代码,望各位大侠指点!谢谢!
...全文
329 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zujun314 2005-10-25
  • 打赏
  • 举报
回复
嘿嘿,我自己弄了个计算时间滴,不知道合理不

<?php
$timer=new timer;
$timer->start();
$con=odbc_connect("MySql_DB","sa","658006",SQL_CUR_USE_ODBC) or die ("数据库连接出错");
odbc_autocommit($con,On);
$sql="Select * from mm order by id desc";
$rs=odbc_exec($con,$sql);
while(odbc_fetch_row($rs))
{
$no=odbc_result($rs,1);
$name=odbc_result($rs,2);
$pwd=odbc_result($rs,3);
echo "编号:  ".$no."  姓名:".$name."  密码:".$pwd."<br>";
}
$mynum=odbc_num_rows($rs);
echo "<br>一共有".$mynum."条数据<br>";
$myfields=odbc_num_fields($rs);
echo "<br>一共有".$myfields."列<br>";
odbc_close($con);

class timer
{
var $startTime=0;
var $endTime=0;
var $spendTime=0;
function start()
{
$this->startTime=microtime();
}
function stop()
{
$this->endTime=microtime();
}
function spend()
{
if($this->spendTime)
{
return $this->spendTime;
}
else
{
$startMicro=substr($this->startTime,0,10);
$startSecond=substr($this->startTime,11,10);
$stopMicro=substr($this->endTime,0,10);
$endSecond=substr($this->endTime,11,10);
$start=doubleval($startMicro) + $startSecond;
$stop=doubleval($stopMicro) + $endSecond;
$this->spendTime=$stop - $start;
return substr($this->spendTime,0,8)."妙";
}
}
}
$timer->stop();
echo "花了好多时间,看看:".$timer->spend();;
?>
ksdiao 2005-10-25
  • 打赏
  • 举报
回复
这个放在一个<?php xxx ?>中,是不是是不是只能计算这一段的时间,如何计算整个php文件的执行时间呢?
iasky 2005-10-25
  • 打赏
  • 举报
回复
晕,写错了在$used_time = $end_time - $start_time;前加个$end_time = timer();
iasky 2005-10-25
  • 打赏
  • 举报
回复
<?php
//时间函数
function timer()
{
$_time = explode( " ", microtime());
$_usec = (double)$_time[0];
$_sec = (double)$_time[1];
return $_timer = $_usec + $_sec;
}
$start_time = timer();
//你的代码



$used_time = $end_time - $start_time;
echo $used_time;
?>
Gdj 2005-10-25
  • 打赏
  • 举报
回复
文件头放一个文件尾放一个不就全文件时间了。如果你连上传文件之类的传输时间也要算的话那就只能在客户端做了。
sanyc 2005-10-25
  • 打赏
  • 举报
回复
都不是很精确啊 呜呜

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧