PHP检测每一段代码执行时间

http://www.12xl.cn/index.php/post/191.html

在写PHP项目的时候,发现速度很慢,于是查看一下是哪里影响的。

  1. <?php
  2. // 实例1
  3.  
  4. /**
  5.  * @start time
  6.  */
  7. function proStartTime() {
  8.     global $startTime;
  9.     $mtime1 = explode(" ", microtime());
  10.     $startTime = $mtime1[1] + $mtime1[0];
  11. }
  12.  
  13. /**
  14.  * @End time
  15.  */
  16. function proEndTime() {
  17.     global $startTime,$set;
  18.     $mtime2 = explode(" ", microtime());
  19.     $endtime = $mtime2[1] + $mtime2[0];
  20.     $totaltime = ($endtime - $startTime);
  21.     $totaltime = number_format($totaltime, 7);
  22.     echo "process time: ".$totaltime."\r\n";
  23. }
  24.  
  25. // 程序调用开始记时
  26. proStartTime();
  27.  
  28. sleep(1);     // sleep() 延时代码执行若干秒
  29. proEndTime(); // 程序在每一段所消耗的执行时间
  30. sleep(2);
  31. proEndTime();
  32. sleep(3);
  33. proEndTime(); 
  34.  
  35.  
  36. /************************************************* 华丽的分割线 **************************************************/
  37.  
  38. // 实例2
  39.  
  40. $t1 = microtime(true);
  41. sleep(3);
  42. $t2 = microtime(true);
  43. echo '程序耗时'.round($t2-$t1,3).'秒';
  44.  
  45. ?>

 

相关推荐

网友评论(0)