建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > PHP学习 > php怎么实现页面代码执行时间

php怎么实现页面代码执行时间

时间:2015-03-17 15:32:44  来源:免费模板网 作者:风雪 阅读次数 tagsphp运用

经常看到一些网站的执行本页面用了多少时间,在php里面怎么实现呢,下面提供了个函数
通过对 PHP计算页面执行时间的代码如下所示:

<?php

  1. class runtime  
  2. {  
  3.     var $StartTime = 0;  
  4.     var $StopTime = 0;  
  5.    
  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.    
  29.    
  30. //例子  
  31. $runtimenew runtime;  
  32. $runtime->start();  
  33.    
  34. //你的代码开始  
  35.    
  36. $a = 0;  
  37. for($i=0; $i<1000000; $i++)  
  38. {  
  39.     $a += $i;  
  40. }  
  41.    
  42. //你的代码结束  
  43.    
  44. $runtime->stop();  
  45. echo "页面执行时间: ".$runtime->spent()." 毫秒";  
  46.    
  47. ?>

通过对PHP计算页面执行时间的代码的了解,新手们应该自己再实际操作一遍,以加深自己的理解。

 

本文地址:https://www.freemoban.com/php/2015/0317/943.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!