timer类,计算脚本的运行时间

源码

class timer {
	var $start;
	var $pause_time;

	/*  start the timer  */
	function timer($start = 0) {
		if($start) { $this->start(); }
	}

	/*  start the timer  */
	function start() {
		$this->start = $this->get_time();
		$this->pause_time = 0;
	}

	/*  pause the timer  */
	function pause() {
		$this->pause_time = $this->get_time();
	}

	/*  unpause the timer  */
	function unpause() {
		$this->start += ($this->get_time() - $this->pause_time);
		$this->pause_time = 0;
	}

	/*  get the current timer value  */
	function get($decimals = 8) {
		return round(($this->get_time() - $this->start),$decimals);
	}

	/*  format the time in seconds  */
	function get_time() {
		list($usec,$sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}
}


运行演示代码:

<?php

include "timer.class.php";

$timer = new timer(1);

/**php代码 开始**/


/**php代码 结束**/

//运行时间
$query_time = $timer->get();
echo $query_time;


/**php代码 开始**/


/**php代码 结束**/

//运行时间
$processing_time = $timer->get();
echo $processing_time;


你可能感兴趣的:(PHP,运行时间)