<?php
class runTime{
private $starTime;
private $stopTime;
private function getMicTime(){
$mictime=microtime();
list($usec,$sec)=explode(' ',$mictime);
return (float)$usec+(float)$sec;
}
public function star(){
$this->starTime=$this->getMicTime();
}
public function stop(){
$this->stopTime=$this->getMicTime();
}
public function spent(){
return round($this->stopTime-$this->starTime)*1000;//单位:毫秒数
}
}
//类使用方法介绍
$time=new runTime();
$time->star();//该语句尽量写在代码段的最开始处
//程序代码段
$time->stop();//该语句最好写在代码段的最结尾处
echo $time->spent();
class runTime{
private $starTime;
private $stopTime;
private function getMicTime(){
$mictime=microtime();
list($usec,$sec)=explode(' ',$mictime);
return (float)$usec+(float)$sec;
}
public function star(){
$this->starTime=$this->getMicTime();
}
public function stop(){
$this->stopTime=$this->getMicTime();
}
public function spent(){
return round($this->stopTime-$this->starTime)*1000;//单位:毫秒数
}
}
//类使用方法介绍
$time=new runTime();
$time->star();//该语句尽量写在代码段的最开始处
//程序代码段
$time->stop();//该语句最好写在代码段的最结尾处
echo $time->spent();
本文介绍了一个简单的PHP类,用于精确测量代码片段的执行时间。通过使用microtime()函数获取当前时间戳并将其拆分为秒和微秒部分,可以计算出程序运行的毫秒数。
1286

被折叠的 条评论
为什么被折叠?



