複製程式碼 程式碼如下:
class Timer {
private $StartTime = 0;//程式運作開始時間
private $StopTime = 0;//程式運作結束時間
private $TimeSpent = 0;//程式執行時間function start(){//程式運作開始
$this->StartTime = microtime();
}
function stop(){//程式運作結束
$this->StopTime = microtime();
}
$this->StopTime = microtime();
} (){//程式運作所花費的時間
if ($this->TimeSpent) {
return $this->TimeSpent;
} else {
list($StartMicro, $StartSecond) = explode(" ", $this-this-this- >StartTime);
list($StopMicro, $StopSecond) = explode(" ", $this->StopTime);
$start = doubleval($StartMicro) + $StartSecond;
$stop = doubleval($StopMicro) + $StartSecond;
$stop = doubleval($StopMicro) + $ StopSecond;
$this->TimeSpent = $stop - $start;
return substr($this->TimeSpent,0,8)."秒";//返回取得到的程式運行時間差
}
}
}
} $timer = new Timer();
$timer->start();
//...程式運作的程式碼
$timer->stop();