最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

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

时间:2014-06-09 16:40来源: 作者: 点击: 次
大家可通常用的microtime()获取页面开始和结束时的时间并相减的话,计算结果是页面运行 所经历的一段时间,但这并不一定是该页面自身运行的时间 实例代码如下: ?php //实例:计算页面运行时加载

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

实例代码如下:

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

使用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. { 
  4.         var $StartTime = 0; 
  5.         var $StopTime = 0; 
  6.         function get_microtime() 
  7.         { 
  8.                 list($usec, $sec) = explode(' ', microtime()); 
  9.                 return ((float)$usec (float)$sec); 
  10.         } 
  11.          
  12.         function start() 
  13.         { 
  14.                 $this->StartTime = $this->get_microtime(); 
  15.         } 
  16.          
  17.         function stop() 
  18.         { 
  19.                 $this->StopTime = $this->get_microtime(); 
  20.         } 
  21.          
  22.         function spent() 
  23.         { 
  24.                 return round(($this->StopTime - $this->StartTime) * 1000, 1); 
  25.         } 
  26. } 
  27. //实例开始 
  28. $runtime= new runtime; 
  29. $runtime->start();  
  30. //你的代码开始 
  31. $a = 0; 
  32. for($i=0; $i<1000000; $i ) 
  33. { 
  34.         $a = $i; 
  35. } 
  36. //你的代码结束 
  37. $runtime->stop(); 
  38. echo "页面执行时间: ".$runtime->spent()." 毫秒"; 
  39. ?> 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容