Home > Article > Backend Development > Interpret the specific code implementation of PHP to calculate page execution time_PHP tutorial
The code for calculating the page execution time by PHP is as follows:
Through understanding of the code used by PHP to calculate page execution time, Novices should practice it again to deepen their understanding.<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 class=""><SPAN>class runtime </SPAN><LI class=alt><SPAN>{ </SPAN><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></SPAN><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></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> function get_microtime() </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> list($usec, $sec) = explode(' ', microtime()); </SPAN><LI class=""><SPAN> return ((float)$usec + (float)$sec); </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> function start() </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> $this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></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><SPAN class=tag-name>1000000</SPAN></FONT></STRONG><SPAN>; $i++) </SPAN></SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN> $a += $i; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//你的代码结束 </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$runtime-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><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>
http://www.bkjia.com/PHPjc/446322.html