首页 > php教程 > php手册 > 解读PHP计算页面执行时间的具体代码实现

解读PHP计算页面执行时间的具体代码实现

WBOY
发布: 2016-06-13 11:06:59
原创
1514 人浏览过

通过对PHP计算页面执行时间的代码如下所示:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>class runtime  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>    var $</span><span class="attribute"><font color="#ff0000">StartTime</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class="alt">
<span>    var $</span><span class="attribute"><font color="#ff0000">StopTime</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class=""><span>   </span></li>
<li class="alt"><span>    function get_microtime()  </span></li>
<li class=""><span>    {  </span></li>
<li class="alt"><span>        list($usec, $sec) = explode(' ', microtime());  </span></li>
<li class=""><span>        return ((float)$usec + (float)$sec);  </span></li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>   </span></li>
<li class="alt"><span>    function start()  </span></li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>        $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">StartTime</font></span><span> = $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get_microtime();  </span>
</li>
<li class=""><span>    }  </span></li>
<li class="alt"><span>   </span></li>
<li class=""><span>    function stop()  </span></li>
<li class="alt"><span>    {  </span></li>
<li class="">
<span>        $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">StopTime</font></span><span> = $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get_microtime();  </span>
</li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>   </span></li>
<li class="alt"><span>    function spent()  </span></li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>        return round(($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>StopTime - $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>StartTime) * 1000, 1);  </span>
</li>
<li class=""><span>    }  </span></li>
<li class="alt"><span>   </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>   </span></li>
<li class=""><span>   </span></li>
<li class="alt"><span>//例子  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">runtime</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> runtime;  </span>
</li>
<li class="alt">
<span>$runtime-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>start();  </span>
</li>
<li class=""><span>   </span></li>
<li class="alt"><span>//你的代码开始  </span></li>
<li class=""><span>   </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class="">
<span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">1000000</span></span></font></strong><span>; $i++)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>    $a += $i;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>   </span></li>
<li class="alt"><span>//你的代码结束  </span></li>
<li class=""><span>   </span></li>
<li class="alt">
<span>$runtime-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>stop();  </span>
</li>
<li class="">
<span>echo "页面执行时间: ".$runtime-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>spent()." 毫秒";  </span>
</li>
<li class="alt"><span>   </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
登录后复制

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


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板