一个计算页面执行时间的简单类

以后可能会扩充些实用的功能

用法:
<?php
include 'stopwatch.php';
$timer = new Stopwatch();
$timer->start();
//这里是要统计执行时间的代码
$timer->stop();
echo $timer->getTime();
?>


<?php
/*
 * @author badboy
 * @2009-06-02
 * class Stopwatch
 * 此类可用于计算程序运行时间
 * 以此观察效率
 */
class Stopwatch
{
	var $previousTime;
	var $timeCost; //所用时间
	
	//constructor
	function Stopwatch()
	{
		$this->timeCost=0;
	}

    function getCurrentTime() 
    { 
        list($usec, $sec) = explode(' ', microtime()); 
        return ((float)$usec + (float)$sec); 
    }	
	
	//start
	function start()
	{
		$this->timeCost = 0;
		$this->previousTime=$this->getCurrentTime();
	}
	
	//stop
	function stop()
	{
		 $this->timeCost = $this->getCurrentTime() - $this->previousTime;
	}
	
	//get time
	function getTime()
	{
		return $this->timeCost;
	}

}
?>

你可能感兴趣的:(PHP)