php中计算页面加载时间几种方法总结

大家可通常用的microtime()获取页面开始和结束时的时间并相减的话,计算结果是页面运行 所经历的一段时间,但这并不一定是该页面自身运行的时间

实例代码如下:

  1. <?php 
  2. //实例:计算页面运行时加载时间 
  3. //分析:页面打开时获取一个时间,加载完成时获取一个时间,运行时间即二者之差 
  4. //1.自定义函数 
  5. function fn(){ 
  6.  list($a,$b) = explode(' ',microtime()); //获取并分割当前时间戳和微妙数,赋值给变量 
  7.  return $a+$b
  8. //2.获取开始时间 
  9. $start_time = fn(); 
  10. //5.加载过程 
  11. for($i=0;$i<10000000;$i++){ 
  12.  // do nothing; 
  13. //3.获取结束时间 
  14. $end_time = fn(); 
  15. //4.计算差值 
  16. echo $end_time$start_time
  17. //5.格式化输出 
  18. echo '<br />'
  19. $t = $end_time$start_time
  20. echo round($t,2); 
  21. ?> 

使用microtime()获取页面开始和结束时的时间并相减的话,计算结果是页面运行 

所经历的一段时间,但这并不一定是该页面自身运行的时间.因为可能存在多个PHP脚 

本页面共同执行的情况,所以我觉得那个方法是不准确的

下面从网上找到一个关于php中计算页面程序运行时间的实例有需要的朋友可参考一下.

最近写了一个程序运行的时间计算类,供大家参考:

实例代码如下:

  1. class Timer {   
  2.     private $StartTime = 0;//程序运行开始时间 
  3.     private $StopTime  = 0;//程序运行结束时间 
  4.     private $TimeSpent = 0;//程序运行花费时间 
  5.     function start(){//程序运行开始 
  6.         $this->StartTime = microtime();   
  7.     }   
  8.     function stop(){//程序运行结束 
  9.         $this->StopTime = microtime();   
  10.     }   
  11.     function spent(){//程序运行花费的时间 
  12.         if ($this->TimeSpent) {   
  13.             return $this->TimeSpent;   
  14.         } else { 
  15.          list($StartMicro$StartSecond) = explode(" "$this->StartTime); 
  16.          list($StopMicro$StopSecond) = explode(" "$this->StopTime); 
  17.             $start = doubleval($StartMicro) + $StartSecond
  18.             $stop = doubleval($StopMicro) + $StopSecond
  19.             $this->TimeSpent = $stop – $start
  20.             return substr($this->TimeSpent,0,8)."秒";//返回获取到的程序运行时间差 
  21.         }   
  22.     }   
  23. }   
  24. $timer = new Timer();   
  25. $timer->start(); 
  26. //…程序运行的代码 
  27. $timer->stop(); 
  28. echo "程序运行时间为:".$timer->spent(); 

再看简化程序 计算页面加载时间

实例代码如下:

  1. <?php 
  2. class runtime 
  3.         var $StartTime = 0; 
  4.         var $StopTime = 0; 
  5.         function get_microtime() 
  6.         { 
  7.                 list($usec$sec) = explode(' ', microtime()); 
  8.                 return ((float)$usec (float)$sec); 
  9.         } 
  10.          
  11.         function start() 
  12.         { 
  13.                 $this->StartTime = $this->get_microtime(); 
  14.         } 
  15.          
  16.         function stop() 
  17.         { 
  18.                 $this->StopTime = $this->get_microtime(); 
  19.         } 
  20.          
  21.         function spent() 
  22.         { 
  23.                 return round(($this->StopTime – $this->StartTime) * 1000, 1); 
  24.         } 
  25. //实例开始 
  26. $runtimenew runtime; 
  27. $runtime->start();  
  28. //你的代码开始 
  29. $a = 0; 
  30. for($i=0; $i<1000000; $i ) 
  31.         $a = $i
  32. //你的代码结束 
  33. $runtime->stop(); 
  34. echo "页面执行时间: ".$runtime->spent()." 毫秒"
  35. ?> 
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php中计算页面加载时间几种方法总结

122 评论

  1. zyban 150mg without prescription strattera for sale buy generic quetiapine 50mg

  2. sertraline 100mg pills lexapro price sildenafil 100mg over the counter

  3. order imuran 100 mcg generic sildenafil otc sildenafil pharmacy

  4. sporanox 100mg generic itraconazole uk buy tindamax generic

  5. buy baclofen online cheap buy zanaflex toradol tablet

  6. purchase viagra sale viagra 100mg uk order tadalafil online cheap

  7. ivermectin 3 mg over the counter dapsone 100 mg us oral dapsone 100 mg

  8. buy diamox 250 mg for sale imdur 20mg canada order imuran 50mg without prescription

  9. amoxil medication amoxil 250mg sale ivermectin 12 mg for humans for sale

  10. modafinil canada provigil order generic lopressor 50mg

  11. buy dutasteride without prescription avodart cost xenical 120mg over the counter

  12. cefdinir 300mg cost cost prevacid order pantoprazole 20mg for sale

  13. buy lexapro online cheap fluoxetine order where can i buy naltrexone

  14. buy crestor 20mg online generic crestor buy motilium tablets

  15. purchase cialis generic mail viagra sildenafil 50mg tablet

  16. diflucan 100mg uk acillin oral order cipro without prescription

  17. purchase azipro online cheap azipro 500mg uk cheap gabapentin pills

  18. asacol 400mg drug astelin us buy cheap avapro

  19. cheap carvedilol cenforce 100mg us order chloroquine 250mg without prescription

  20. buy nifedipine 30mg without prescription allegra canada fexofenadine price

  21. oral sumycin 500mg baclofen order purchase lioresal for sale

  22. order ampicillin 500mg generic buy ampicillin flagyl price

  23. buy toradol 10mg generic buy inderal 10mg purchase inderal for sale

  24. buy erythromycin 500mg for sale tamoxifen 10mg drug where to buy nolvadex without a prescription

  25. molnupiravir 200 mg pill order naproxen cefdinir 300 mg brand

  26. order glucotrol online cheap glipizide 5mg price order betamethasone 20gm online

  27. order tacrolimus for sale cost remeron purchase requip online cheap

  28. buy captopril 25mg for sale buy captopril 25mg buy carbamazepine 400mg without prescription

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡