很简单的类 主要功能有来两个 记录运行时间 记录内存使用情况
代码and注释:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Benchmark {
var $marker = array();
// 打时间mark 记录当时的时间
function mark($name)
{
$this->marker[$name] = microtime();
}
// 计算运行时间
function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
{
if ($point1 == '')
{
return '{elapsed_time}';
}
if ( ! isset($this->marker[$point1]))
{
return '';
}
if ( ! isset($this->marker[$point2]))
{
$this->marker[$point2] = microtime();
}
list($sm, $ss) = explode(' ', $this->marker[$point1]);
list($em, $es) = explode(' ', $this->marker[$point2]);
return number_format(($em + $es) - ($sm + $ss), $decimals);
}
// 内存使用
function memory_usage()
{
return '{memory_usage}';
}
}
Code Tips:
时间计算中 时间记录都是取的 microtime 返回的是 空格隔开的秒与毫秒 框架的运行时间都是毫秒级别
为了计算要拆分出毫秒
CI 的处理方式(写法)值得学习
list($sm, $ss) = explode(' ', $this->marker[$point1]);
list($em, $es) = explode(' ', $this->marker[$point2]);
return number_format(($em + $es) - ($sm + $ss), $decimals);