這段時間在作項目的後端服務優化,數據量比較大的兩個後臺服務一個是首頁微博列表獲取,一個是我的動態的獲取,先從性能來分析,須要用到時間統計,因此抽時間寫了一個函數的時間消耗統計類,實現上比較簡單,可是還算好用,分享給你們,若是有錯誤或者能夠改進的話歡迎指出。 我統計的結果,wifi環境下基本上拉取16條數據須要3~4s,這個數據仍是至關高了,接下來找找方法看看能不能優化。 使用舉例: <?php require_once 'TimeHelper.class.php'; $myTimeHelper = new TimeHelper(); #代碼段A $myTimeHelper->recordNow("代碼A消耗"); #代碼段B $myTimeHelper->recordNow("代碼B消耗"); $myTimeHelper->printInfo(); ?> TimeHelper.class.php <?php // +---------------------------------------------------------------------- // | Copyright (c) 2014 doBell www.dobell.me // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: congorz <congorz@yeah.net> // +---------------------------------------------------------------------- /** * doSchool-服務器端 服務消耗時間統計API * @author congorz <congorz@yeah.net> * @lastdate 2014年12月25日19:51:31 */ class TimeHelper{ private $startTime; private $tempTime; private $spendTime; private $recordString; public function __construct() { $this->startTime = microtime(true); $this->tempTime = array(); $this->spendTime = array(); $this->recordStrs = array(); $this->tempTime[] = $this->startTime; } public function recordNow($str) { //str是對於前面代碼功能的註釋標籤 $this->recordStrs[] = $str; $this->tempTime[] = microtime(true); } public function handle() { $count = count($this->tempTime); $total = 0; for ($i=1; $i < $count; $i++) { $oneSpend = $this->tempTime[$i] - $this->tempTime[$i-1]; $str_oneSpend = var_export($oneSpend, TRUE); if(substr_count($str_oneSpend,"E")){ //科學計數法的處理,暫時沒作 } $total += $oneSpend; $tempStr = $this->recordStrs[$i-1]; $this->spendTime["$tempStr"] = $oneSpend; } $this->spendTime["總計時間"] = $total; } public function printInfo() { $this->handle(); print_r($this->spendTime); } } ?>