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

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

WBOY
WBOY원래의
2016-06-13 11:06:591509검색

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.