Discuz教程网

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

[复制链接]
authicon dly 发表于 2011-1-12 09:11:58 | 显示全部楼层 |阅读模式
全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点,需要的朋友可以参考下。
代码如下:

  1. // 定义全局变量 记录时间
  2. $_timer_id = 0;
  3. // 函数设置全局变量 记录各个断点的运行所需时间
  4. function makeTimer( $notes, $onOff=FALSE )
  5. {
  6. if( $onOff )
  7. {
  8. global $_timer_id;
  9. $GLOBALS[\'timer\'][$_timer_id][0] = microtime(TRUE);
  10. $GLOBALS[\'timer\'][$_timer_id][1] = $notes;
  11. $_timer_id++;
  12. }
  13. }
  14. // 把全局运行时间情况输出
  15. function traceTimer()
  16. {
  17. $timer_str = \'\';
  18. $G_timer = count($GLOBALS[\'timer\'])-1;
  19. if( $G_timer>0 )
  20. {
  21. for( $i=0;$i<$G_timer;$i++ )
  22. {
  23. $dif_time = number_format( ($GLOBALS[\'timer\'][$i+1][0] - $GLOBALS[\'timer\'][$i][0]), 3 );
  24. $timer_str .= \'dif: \'.$dif_time.\' \'.$GLOBALS[\'timer\'][$i][1]."\\n";
  25. }
  26. $dif_time = number_format( (microtime(TRUE) - $GLOBALS[\'timer\'][$G_timer][0]), 3 );
  27. $timer_str .= \'dif: \'.$dif_time.\' \'.$GLOBALS[\'timer\'][$G_timer][1]."\\n";
  28. }
  29. return $timer_str;
  30. }
  31. 使用方法:
  32. // 开始时间
  33. makeTimer( \' LINE:\'.__LINE__ );
  34. $imgstrpos = strpos($str, \'<img\'.$imgstr);
  35. makeTimer( \' LINE:\'.__LINE__ );
  36. $str_p = substr($str_noimg, 0, $imgstrpos);
  37. makeTimer( \' LINE:\'.__LINE__ );
  38. $str_n = substr($str_noimg, $imgstrpos, strlen($str_noimg));
  39. makeTimer( \' LINE:\'.__LINE__ );
  40. $pst_exc_imgs = $str_p.\'<img \'.$imgstr.\'>\'.$str_n." ";
  41. makeTimer( \' LINE:\'.__LINE__ );
  42. // 记录到日志中
  43. error_log( traceTimer(), 3, \'/tmp/\'.basename(__FILE__).\'.log\' );
  44. // 或者直接输出
  45. echo traceTimer();
复制代码






上一篇:php循环检测目录是否存在并创建
下一篇:1314学习网 服务条款
authicon icenna 发表于 2011-6-17 22:59:50 | 显示全部楼层
支持楼主,顶一下
authicon mjz 发表于 2011-6-22 23:00:07 | 显示全部楼层
很好 很强大。。谢谢分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 09:26

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表