復制代碼 代碼如下:
class Timer {
private $StartTime = 0;//程序運行開始時間
private $StopTime = 0;//程序運行結束時間
private $TimeSpent = 0;//程序運行花費時間
function start(){//程序運行開始
$this->StartTime = microtime();
}
function stop(){//程序運行結束
$this->StopTime = microtime();
}
function spent(){//程序運行花費的時間
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)."秒";//返回獲取到的程序運行時間差
}
}
}
$timer = new Timer();
$timer->start();
//...程序運行的代碼
$timer->stop();
echo "程序運行時間為:".$timer->spent();