欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-04 13:07:15
...
以后可能会扩充些实用的功能

用法:

<?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