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

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中substr_count()函数获取子字符串出现次数的方法

本文实例讲述了PHP中substr_count()函数获取子字符串出现次数的方法。分享给大家供大家参考,具体如下: PHP中的substr_count()可用于计算指定字符串中子字符串出...

php判断是否为ajax请求的方法

本文实例讲述了php判断是否为ajax请求的方法。分享给大家供大家参考,具体如下: 先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名...

由php if 想到的些问题

复制代码 代码如下:<?php  /* PHP code */  header("Content-type: text/...

php中替换字符串函数strtr()和str_repalce()的用法与区别

首先来看看这个php字符串替换函数 strtr()的两种用法: strtr(string,from,to) 或者strtr(string,array) 首先针对strtr函数第一种方式:...

PHP 7.1中利用OpenSSL代替Mcrypt加解密的方法详解

PHP 7.1中利用OpenSSL代替Mcrypt加解密的方法详解

概要: php7.1发布后新特性吸引了不少PHPer,大家都在讨论新特性带来的好处与便利。但是从php7.0 升级到 php7.1 废弃(过时)了一个在过去普遍应用的扩展(mcrypt...