首页 > 网站 > 帮助中心 > 正文

php代码怎么运行时间?PHP一个页面执行时间类代码

2024-07-09 22:39:19
字体:大 中 小
来源:转载
供稿:网友

核心代码:

  1. <?php 
  2. classTimer//页面执行时间类 
  3. { 
  4. varstarttime;//页面开始执行时间 
  5. varstoptime;//页面结束执行时间 
  6. varspendtime;//页面执行花费时间 
  7. functiongetmicrotime()//获取返回当前微秒数的浮点数 
  8. { 
  9. list(usec,sec)=<a href="/tags.php/explode/" target="_blank">explode</a>(" ",microtime()); 
  10. return((float)usec + (float)sec); 
  11. } 
  12. functionstart()//页面开始执行函数,返回开始页面执行的时间 
  13. { 
  14. this->starttime=this->getmicrotime(); 
  15. } 
  16. functiondisplay()//显示页面执行的时间 
  17. { 
  18. this->stoptime=this->getmicrotime(); 
  19. this->spendtime=this->stoptime-this->starttime; 
  20. returnround(this->spendtime,10); 
  21. } 
  22. } 
  23. /*调用方法 
  24. timer=new Timer(); 
  25. timer->start(); 
  26. /*在此处放入你要执行的脚本或代码 
  27. for(i=0;i<100000;i++) 
  28. { 
  29. echo i; 
  30. echo "<br>"; 
  31. } 
  32. */ 
  33. //echo " 
  34. 执行该代码花费时间".timer->display()."秒"; 
  35. ?> 

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

  1. <?php 
  2. // 实例1 
  3.   
  4. /** 
  5.  * @start time 
  6.  */ 
  7. functionproStartTime() { 
  8.   global$startTime; 
  9.   $mtime1=explode(" ", microtime()); 
  10.   $startTime=$mtime1[1] +$mtime1[0]; 
  11. } 
  12.   
  13. /** 
  14.  * @End time 
  15.  */ 
  16. functionproEndTime() { 
  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"<br>process time: ".$totaltime; 
  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. //Vevb.com 
  40. $t1= microtime(true); 
  41. sleep(3); 
  42. $t2= microtime(true); 
  43. echo'程序耗时'.round($t2-$t1,3).'秒' 
  44.   
  45. ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表