首页 > 技术文章 > PHP计算程序运行时间的类

qhorse 2015-09-25 15:25 原文

<?php
class runTime{
	private $starTime;
	private $stopTime;
	private function getMicTime(){
		$mictime=microtime(true);
		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();

推荐阅读