首页 > php知识 > 获取脚本执行时间的php类 – 含有暂停功能

获取脚本执行时间的php类 – 含有暂停功能

2010年3月9日

php-class-time-tracking
上一篇文章,给大家介绍了一个获取脚本执行时间的php类, 今天再给大家介绍一个。今天的这个获取脚本执行时间的php类的特别之处是它提供了“暂停|开启”功能,可以让我们很方便的跳过某些代码的执行时间。

举个例子,当我们只想知道页面中SQL的执行时间是多长,我们就可以在每次进行SQL查询之前,开始计时,查询之后暂停计时,然后开始处理数据,下一次进行SQL查询时再开始计时…这样最终的执行时间,就是我们想要的SQL执行时间了。

下面看一下代码

[php]
<?php

/**
* Timer class
* Created: 27th January 2010
* Copyright Jay Gilford 2010
* http://www.jaygilford.com
* email: jay [at] jaygilford.com
* Translate By: 10V 2010
* http://www.smartwei.com
* email: drinkthere@gmail.com
**/

class timer {
private $_start = 0;
private $_elapsed = 0;
private $_running = false;

/**
* timer::construct()
*
* @param bool $start
* @return true
*/
public function __construct($start = false) {
// $star为true时, 开始计时
if($start === true)
$this->start();

return true;
}

/**
* timer::start()
* 开始计时
*
* @return true
*/
public function start() {
// 重置计时器
$this->reset(false);
// 设置计时状态
$this->_running = true;
// 设置当前时间
$this->_start = $this->_time();

return true;
}

/**
* timer::stop()
* 停止计时,并返回执行时间
*
* @return mixed
*/
public function stop() {
// 如果计时器没有计时 return false
if($this->_running === false)
return false;

// 停止计时器
$this->_elapsed = $this->_get_elapsed();
$this->_running = false;
$this->_start = 0;

// 返回 执行时间
return $this->_elapsed;
}

/**
* timer::pause()
* 暂停|开始 计时
*
* @return NULL
*/
public function pause() {
// 如果计时器没有计时 return false
if($this->_running === false)
return false;

// 如果暂停计时器, 将计时器的开始时间设置为当前时间
if($this->_start == 0) {
$this->_start = $this->_time();

// 否则将已执行的时间计入总执行时间,并暂停计时器
}else{
$this->_elapsed = $this->_get_elapsed();
$this->_start = 0;
}
}

/**
* timer::elapsed()
* 返回总执行时间
*
* @return float
*/
public function elapsed() {
return $this->_get_elapsed();
}

/**
* timer::reset()
* 将计时器的变量置为默认值
*
* @param bool $start
* @return
*/
public function reset($start = false) {
// 重置变量
$this->_start = 0;
$this->_elapsed = 0;
$this->_running = false;

// 如果$start为ture,开始计时
if($start === true)
$this->start();

return true;
}

/**
* timer::_time()
* 返回当前时间戳(含有微秒)
*
* @return float
*/
private function _time() {
$time = explode(‘ ‘, microtime());
$time = (float)$time[1] + $time[0];
return $time;
}

/**
* timer::_get_elapsed()
* 返回执行时间
*
* @return float
*/
private function _get_elapsed() {
if($this->_running === false || $this->_start == 0)
return $this->_elapsed;

return $this->_elapsed + ($this->_time() – $this->_start);
}
}
?>
[/php]

看一下列子:

[php]
<?php

include ‘timer.class.php’;
$db_timer = new timer();

// 这里只是简单举个例子,SQL语句只是表意,请根据你的实际情况来替换
mysql_connect(‘localhost’, ‘root’, ”);
mysql_select_db(‘database’);

$query = "SELECT * FROM table";

// 在查询前开始计时
$db_timer->start();
$result = mysql_query($query);

// 查询完毕,暂停计时
$db_timer->pause();

// 处理数据
while($row = mysql_fetch_assoc($result)) {
echo print_r($row, true);
}

$query = "SELECT * FROM table2";
// 在下一次查询之前,开启计时器
$db_timer->pause();

// 执行查询
$result = mysql_query($query);

// 停止计时器,获得SQL查询语句的执行时间
$total_time = $db_timer->stop();

// 显示详细信息
echo ‘SQL查询总共执行了: ‘.$total_time.’ 秒’;
?>
[/php]

这个提供“暂停|开启” 功能的获取脚本执行时间php类就介绍到这里了,脚本的代码很简单,大家可以亲自试一下

作者: 石巍
原载: 10V
版权所有,转载时必须以超链接形式注明作者和原始出处及本声明。

本文链接: http://www.smartwei.com/support-pause-script-time-tracking.html

本文的评论功能被关闭了.