全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

// 定义全局变量 记录时间
$_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实现的回溯算法示例

本文实例讲述了PHP实现的回溯算法。分享给大家供大家参考,具体如下: 问题:  一头大牛驼2袋大米,一头中牛驼一袋大米,两头小牛驼一袋大米,请问100袋大米需要多少头大牛,...

部署PHP项目应该注意的几点事项分享

在部署PHP项目时,有几点需要特别注意,也是初学者比较容易忽视的点: 一、下载WAMPServer后,如何安装配置? 二、如何通过客户端管理MySQL数据库? 三、如何通过IP地址访问P...

PHP中的自动加载操作实现方法详解

本文实例讲述了PHP中的自动加载操作实现方法。分享给大家供大家参考,具体如下: what is 自动加载? 或许你已经对自动加载有所了解。简单描述一下:自动加载就是我们在new一个cla...

如何用php获取程序执行的时间

在head.htm中加入,也就是在默认模版中添加“$stime=microtime(true); //获取程序开始执行的时间”复制代码 代码如下:<!--<?php$stim...