全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
作者:bea
代码如下: // 定义全局变量 记录时间 $_timer_id = 0; // 函数设置全局变量 记录各个断点的运行所需时间 function makeTimer( $notes, $onOff=FALSE ) { if( $onOff ) { global $_timer_id; $GLOBALS['timer'][$_timer_id][0] = microtime(TRUE); $GLOBALS['timer'][$_timer_id][1] =
代码如下:
// 定义全局变量 记录时间
$_timer_id = 0;
// 函数设置全局变量 记录各个断点的运行所需时间
function makeTimer( $notes, $onOff=FALSE )
{
if( $onOff )
{
global $_timer_id;
$GLOBALS['timer'][$_timer_id][0] = microtime(TRUE);
$GLOBALS['timer'][$_timer_id][1] = $notes;
$_timer_id++;
}
}
// 把全局运行时间情况输出
function traceTimer()
{
$timer_str = '';
$G_timer = count($GLOBALS['timer'])-1;
if( $G_timer>0 )
{
for( $i=0;$i<$G_timer;$i++ )
{
$dif_time = number_format( ($GLOBALS['timer'][$i+1][0] - $GLOBALS['timer'][$i][0]), 3 );
$timer_str .= 'dif: '.$dif_time.' '.$GLOBALS['timer'][$i][1]."
";
}
$dif_time = number_format( (microtime(TRUE) - $GLOBALS['timer'][$G_timer][0]), 3 );
$timer_str .= 'dif: '.$dif_time.' '.$GLOBALS['timer'][$G_timer][1]."
";
}
return $timer_str;
}
使用方法:
// 开始时间
makeTimer( ' LINE:'.__LINE__ );
$imgstrpos = strpos($str, '<img'.$imgstr);
makeTimer( ' LINE:'.__LINE__ );
$str_p = substr($str_noimg, 0, $imgstrpos);
makeTimer( ' LINE:'.__LINE__ );
$str_n = substr($str_noimg, $imgstrpos, strlen($str_noimg));
makeTimer( ' LINE:'.__LINE__ );
$pst_exc_imgs = $str_p.'<img '.$imgstr.'>'.$str_n." ";
makeTimer( ' LINE:'.__LINE__ );
// 记录到日志中
error_log( traceTimer(), 3, '/tmp/'.basename(__FILE__).'.log' );
// 或者直接输出
echo traceTimer();
at 2010-05-14 09:20
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- PHP中用正则表达式清除字符串的空白
- php开发环境配置记录
- PHP文件读写操作之文件写入代码
- PHP文件读写操作之文件读取方法详解
- PHP目录函数实现创建、读取目录教程实例
- PHP开发规范手册之PHP代码规范详解
- PHP JSON格式数据交互实例代码详解
- PHP学习笔记之二 php入门知识
- php算开始时间到过期时间的相隔的天数
- php数据库密码的找回的步骤
- 重新封装zend_soap实现http连接安全认证的php代码
- php 变量未定义等错误的解决方法
- 兼容性比较好的PHP生成缩略图的代码
- php的日期处理函数及uchome的function_coomon中日期处理函数的研究
- PHP日期处理函数 整型日期格式
- Base64在线编码解码实现代码 演示与下载
- php !function_exists("T7FC56270E7A70FA81A5935B72EACBE29"))代码解密
- PHP备份/还原MySQL数据库的代码
- php循环检测目录是否存在并创建(循环创建目录)