/system/core/Benchmark.php 定时器基类标记点和计算时间差

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 * 一个开源PHP5.1.6或更新版本的应用程序开发框架
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Benchmark Class
 * 笨基准类
 * 
 * This class enables you to mark points and calculate the time difference
 * between them.  Memory consumption can also be displayed.
 *
 * 这个类可以让您标记点和计算时间差在它们之间。也可以显示的内存消耗。
 *
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/benchmark.html
 */
class CI_Benchmark {

	/**
	 * List of all benchmark markers and when they were added
	 * 所有基准标记列表,当它们被添加
	 * @var array
	 */
	var $marker = array();

	// --------------------------------------------------------------------

	/**
	 * Set a benchmark marker
	 * 设置基准标记
	 * 
	 * Multiple calls to this function can be made so that several
	 * execution points can be timed
	 * 调用这个函数可以使一些可以定时执行点
	 * @access	public
	 * @param	string	$name	name of the marker 标记的名称
	 * @return	void
	 */
	function mark($name)
	{
		$this->marker[$name] = microtime();
	}

	// --------------------------------------------------------------------

	/**
	 * Calculates the time difference between two marked points.
	 * 计算两个标记点之间的时间差。
	 * 
	 * 
	 * If the first parameter is empty this function instead returns the
	 * {elapsed_time} pseudo-variable. This permits the full system
	 * execution time to be shown in a template. The output class will
	 * swap the real value for this variable.
	 *
	 * 如果第一个参数为空,此函数而是返回
     * {ELAPSED_TIME}伪变量。这允许整个系统执行时间显示在一个模板。
     * 输出类交换真正的此变量的值。
	 *
	 *
	 * @access	public
	 * @param	string	a particular marked point    一个特定的标记点
	 * @param	string	a particular marked point    一个特定的标记点
	 * @param	integer	the number of decimal places 的小数位数
	 * @return	mixed
	 */
	
	//echo "microtime():".microtime(); 0.42883700 1368023943
	//microtime() 函数返回当前Unix时间戳和微秒数
	function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
	{
		//如果第一个特定的标记点为空,直接返回{elapsed_time}
		if ($point1 == '')
		{
			return '{elapsed_time}';
		}
        //如果在当前标记数组中找不到第一个标记点的话,直接返回空
		if ( ! isset($this->marker[$point1]))
		{
			return '';
		}
        //如果找不到第二个标记点,那么将第二个标记点保存到marker数组中
		if ( ! isset($this->marker[$point2]))
		{
			$this->marker[$point2] = microtime();
		}
        //$sm=时间戳  $ss=微秒数
		list($sm, $ss) = explode(' ', $this->marker[$point1]);
		list($em, $es) = explode(' ', $this->marker[$point2]);
        
		//number_format()函数通过千位分组来格式化数字
		
		//number_format(number,decimals,decimalpoint,separator)
		//number:必需,要格式化的数字,如果未设置其他参数,则数字会被格式化为不带小数点且以逗号,作为分隔符
		//decimals:可选,规定多少个小数,如果设置了该参数,则使用点号.作为小数点来格式化数字
		return number_format(($em + $es) - ($sm + $ss), $decimals);
	}

	// --------------------------------------------------------------------

	/**
	 * Memory Usage
	 * 内存使用情况
	 * 
	 * This function returns the {memory_usage} pseudo-variable.
	 * This permits it to be put it anywhere in a template
	 * without the memory being calculated until the end.
	 * The output class will swap the real value for this variable.
	 *
	 * 这个函数返回{memory_usage}伪变量。
     * 这使得它可以在任何地方把它在一个模板
     * 没有记忆直到最后计算。
     * 输出类会掉这个变量的真正价值。
	 *
	 *
	 * @access	public
	 * @return	string
	 */
	function memory_usage()
	{
		return '{memory_usage}';
	}

}

// END CI_Benchmark class

/* End of file Benchmark.php */
/* Location: ./system/core/Benchmark.php */

  

posted @ 2013-05-08 22:45  简单--生活  阅读(237)  评论(0)    收藏  举报
简单--生活(CSDN)