查看: 5806|回复: 3
打印 上一主题 下一主题

php计算页面执行时间

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-5 11:36:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
class timer
{
var $StartTime = 0;
var $StopTime = 0;
var $TimeSpent = 0;

function start(){
$this->StartTime = microtime();}
function stop(){
$this->StopTime = microtime();}
function spent(){
if ($this->TimeSpent) {
return $this->TimeSpent;
} else {
$StartMicro = substr($this->StartTime,0,10);
$StartSecond = substr($this->StartTime,11,10);
$StopMicro  = substr($this->StopTime,0,10);
$StopSecond = substr($this->StopTime,11,10);
$start = doubleval($StartMicro) + $StartSecond;
$stop = doubleval($StopMicro) + $StopSecond;
$this->TimeSpent = $stop - $start;
return substr($this->TimeSpent,0,8)."秒";
}
}// end function spent();
}//end class timer;
//例子
$timer = new timer;
$timer->start();
/*
你的代码放在此处
*/

$timer->stop();
echo "执行本SCRIPT共".$timer->spent();
?>
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
2#
 楼主| 发表于 2007-10-5 11:37:05 | 只看该作者
台州网址导航
计算页面执行时间

在PHP网页的开头加入以下

<?

$time_start = getmicrotime();

function getmicrotime()

{

list($usec, $sec) = explode(" ",microtime());

return ((float)$usec + (float)$sec);

}

?>

然后到最后加入以下代码

<?

$time_end = getmicrotime();

printf ("[页面执行时间: %.2f毫秒]\n\n",($time_end-$time_start)) ;
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
3#
 楼主| 发表于 2007-10-5 11:37:33 | 只看该作者
台州网址导航

php页面执行时间类

  1. <?php
  2. class Timer//页面执行时间类
  3. {
  4.     /**Filename : timer.class.php
  5.     *  Function : 计算页面执行时间类
  6.     *  Author   : forest(木林森)
  7.     *  E-mail   : chaizuxue@163.com
  8.     *  Date     : 2007-02-15
  9.     */
  10.     var $starttime;//页面开始执行时间
  11.     var $stoptime;//页面结束执行时间
  12.     var $spendtime;//页面执行花费时间
  13.    
  14.     function getmicrotime()//获取返回当前微秒数的浮点数
  15.     {
  16.         list($usec,$sec)=explode(" ",microtime());
  17.         return ((float)$usec + (float)$sec);
  18.     }
  19.    
  20.     function start()//页面开始执行函数,返回开始页面执行的时间
  21.     {
  22.         $this->starttime=$this->getmicrotime();
  23.     }
  24.    
  25.     function display()//显示页面执行的时间
  26.     {
  27.         $this->stoptime=$this->getmicrotime();
  28.         $this->spendtime=$this->stoptime-$this->starttime;
  29.         return round($this->spendtime,10);
  30.     }
  31. }
  32. /*调用方法
  33. $timer=new Timer();
  34. $timer->start();
  35. /*在此处放入你要执行的脚本或代码
  36. for($i=0;$i<100000;$i++)
  37. {
  38.     echo $i;
  39.     echo "
  40. ";
  41. }
  42. */
  43. //echo "执行该代码花费时间".$timer->display()."秒";
  44. ?>
复制代码
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
4#
 楼主| 发表于 2007-10-5 11:37:44 | 只看该作者
台州网址导航
  1. <?
  2. class timer {
  3.   var $StartTime = 0;
  4.   var $StopTime = 0;
  5.   var $TimeSpent = 0;
  6.   function start(){
  7.     $this->StartTime = microtime();
  8.   }
  9.   function stop(){
  10.     $this->StopTime = microtime();
  11.   }
  12.   function spent() {
  13.     if ($this->TimeSpent) {
  14.       return $this->TimeSpent;
  15.     } else {
  16.       $StartMicro = substr($this->StartTime,0,10);
  17.       $StartSecond = substr($this->StartTime,11,10);
  18.       $StopMicro = substr($this->StopTime,0,10);
  19.       $StopSecond = substr($this->StopTime,11,10);
  20.       $start = doubleval($StartMicro) + $StartSecond;
  21.       $stop = doubleval($StopMicro) + $StopSecond;
  22.       $this->TimeSpent = $stop - $start;
  23.       return substr($this->TimeSpent,0,8)."秒";
  24.     }
  25.   } // end function spent();
  26. } //end class timer;
  27. //例子:
  28.   $timer = new timer;
  29.   $timer->start();
  30. /*
  31. 你的代码放在此处
  32. */
  33.   $timer->stop();
  34.   echo "执行本SCRIPT共".$timer->spent();
  35. ?>
复制代码
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表