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