手机
当前位置:查字典教程网 >编程开发 >php教程 >全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
摘要:复制代码代码如下://定义全局变量记录时间$_timer_id=0;//函数设置全局变量记录各个断点的运行所需时间functionmakeT...

复制代码 代码如下:

// 定义全局变量 记录时间

$_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]."n";

}

$dif_time = number_format( (microtime(TRUE) - $GLOBALS['timer'][$G_timer][0]), 3 );

$timer_str .= 'dif: '.$dif_time.' '.$GLOBALS['timer'][$G_timer][1]."n";

}

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程序

用Apache反向代理设置对外的WWW和文件服务器

PHP5试用(二)

JAVA/JSP学习系列之四

php使用cookie实现记住登录状态

php实现首页自动选择语言转跳

如何对PHP程序中的常见漏洞进行攻击

精品推荐
分类导航