站长论坛
标题:
php计算页面执行时间
[打印本页]
作者:
webptr
时间:
2007-10-5 11:36
标题:
php计算页面执行时间
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();
?>
作者:
webptr
时间:
2007-10-5 11:37
计算页面执行时间
在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)) ;
作者:
webptr
时间:
2007-10-5 11:37
标题:
php页面执行时间类
<?php
class Timer//页面执行时间类
{
/**Filename : timer.class.php
* Function : 计算页面执行时间类
* Author : forest(木林森)
* E-mail : chaizuxue@163.com
* Date : 2007-02-15
*/
var $starttime;//页面开始执行时间
var $stoptime;//页面结束执行时间
var $spendtime;//页面执行花费时间
function getmicrotime()//获取返回当前微秒数的浮点数
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function start()//页面开始执行函数,返回开始页面执行的时间
{
$this->starttime=$this->getmicrotime();
}
function display()//显示页面执行的时间
{
$this->stoptime=$this->getmicrotime();
$this->spendtime=$this->stoptime-$this->starttime;
return round($this->spendtime,10);
}
}
/*调用方法
$timer=new Timer();
$timer->start();
/*在此处放入你要执行的脚本或代码
for($i=0;$i<100000;$i++)
{
echo $i;
echo "
";
}
*/
//echo "执行该代码花费时间".$timer->display()."秒";
?>
复制代码
作者:
webptr
时间:
2007-10-5 11:37
<?
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();
?>
复制代码
欢迎光临 站长论坛 (http://www.tzlink.com/bbs/)
Powered by Discuz! X3.2