PHP測試程序運行時間的類
更新時間:2012年02月05日 21:54:52 作者:
今天,為了測試一斷程序的運行時間,特意寫了這么一個類,并添加了注釋,希望對大家有所幫助
類很簡單,主要是運用了幾個函數(shù)數(shù)組列表函數(shù)list(),字符串分割成數(shù)組函數(shù)explode(),獲取時間戳和微秒數(shù)microtime(),代碼如下:
<?php
class runTime {
private $starTime;//開始時間
private $stopTime;//結(jié)束時間
private function getMicTime(){
$mictime=microtime();//獲取時間戳和微秒數(shù)
list($usec,$sec)=explode(" ",$mictime);//把微秒數(shù)分割成數(shù)組并轉(zhuǎn)換成變量處理
return (float)$usec+(float)$sec;//把轉(zhuǎn)換后的數(shù)據(jù)強制用浮點點來處理
}
public function star(){//獲取開始時間
$this->starTime=$this->getMicTime();
}
public function stop(){//獲取結(jié)束時間
$this->stopTime=$this->getMicTime();
}
public function spent(){//計算程序持續(xù)時間
return round($this->stopTime-$this->starTime)*1000;//獲取毫秒數(shù)
}
}
//舉例
$time=new runTime();
$time->star();
for ($i=1;$i<=1000;$i++){
echo("a");
}
$time->stop();
echo $time->spent();
?>
復(fù)制代碼 代碼如下:
<?php
class runTime {
private $starTime;//開始時間
private $stopTime;//結(jié)束時間
private function getMicTime(){
$mictime=microtime();//獲取時間戳和微秒數(shù)
list($usec,$sec)=explode(" ",$mictime);//把微秒數(shù)分割成數(shù)組并轉(zhuǎn)換成變量處理
return (float)$usec+(float)$sec;//把轉(zhuǎn)換后的數(shù)據(jù)強制用浮點點來處理
}
public function star(){//獲取開始時間
$this->starTime=$this->getMicTime();
}
public function stop(){//獲取結(jié)束時間
$this->stopTime=$this->getMicTime();
}
public function spent(){//計算程序持續(xù)時間
return round($this->stopTime-$this->starTime)*1000;//獲取毫秒數(shù)
}
}
//舉例
$time=new runTime();
$time->star();
for ($i=1;$i<=1000;$i++){
echo("a");
}
$time->stop();
echo $time->spent();
?>
相關(guān)文章
php數(shù)組函數(shù)序列之a(chǎn)sort() - 對數(shù)組的元素值進行升序排序,保持索引關(guān)系
asort() 函數(shù)對數(shù)組進行排序并保持索引關(guān)系。主要用于對那些單元順序很重要的結(jié)合數(shù)組進行排序。可選的第二個參數(shù)包含了附加的排序標(biāo)識2011-11-11php與XML、XSLT、Mysql的結(jié)合運用實現(xiàn)代碼
php與XML、XSLT、Mysql的結(jié)合運用實現(xiàn)代碼,需要的朋友可以參考下。2009-11-11