復(fù)制代碼 代碼如下:
class Timer {
private $StartTime = 0;//程序運(yùn)行開始時(shí)間
private $StopTime = 0;//程序運(yùn)行結(jié)束時(shí)間
private $TimeSpent = 0;//程序運(yùn)行花費(fèi)時(shí)間
function start(){//程序運(yùn)行開始
$this->StartTime = microtime();
}
function stop(){//程序運(yùn)行結(jié)束
$this->StopTime = microtime();
}
function spent(){//程序運(yùn)行花費(fèi)的時(shí)間
if ($this->TimeSpent) {
return $this->TimeSpent;
} else {
list($StartMicro, $StartSecond) = explode(" ", $this->StartTime);
list($StopMicro, $StopSecond) = explode(" ", $this->StopTime);
$start = doubleval($StartMicro) + $StartSecond;
$stop = doubleval($StopMicro) + $StopSecond;
$this->TimeSpent = $stop - $start;
return substr($this->TimeSpent,0,8)."秒";//返回獲取到的程序運(yùn)行時(shí)間差
}
}
}
$timer = new Timer();
$timer->start();
//...程序運(yùn)行的代碼
$timer->stop();
echo "程序運(yùn)行時(shí)間為:".$timer->spent();
新聞熱點(diǎn)
疑難解答