核心代碼:
- <?php
- classTimer//頁面執行時間類
- {
- varstarttime;//頁面開始執行時間
- varstoptime;//頁面結束執行時間
- varspendtime;//頁面執行花費時間
- functiongetmicrotime()//獲取返回當前微秒數的浮點數
- {
- list(usec,sec)=<a href="/tags.php/explode/" target="_blank">explode</a>(" ",microtime());
- return((float)usec + (float)sec);
- }
- functionstart()//頁面開始執行函數,返回開始頁面執行的時間
- {
- this->starttime=this->getmicrotime();
- }
- functiondisplay()//顯示頁面執行的時間
- {
- this->stoptime=this->getmicrotime();
- this->spendtime=this->stoptime-this->starttime;
- returnround(this->spendtime,10);
- }
- }
- /*調用方法
- timer=new Timer();
- timer->start();
- /*在此處放入你要執行的腳本或代碼
- for(i=0;i<100000;i++)
- {
- echo i;
- echo "<br>";
- }
- */
- //echo "
- 執行該代碼花費時間".timer->display()."秒";
- ?>
PHP檢測每一段代碼執行時間:
- <?php
- // 實例1
- /**
- * @start time
- */
- functionproStartTime() {
- global$startTime;
- $mtime1=explode(" ", microtime());
- $startTime=$mtime1[1] +$mtime1[0];
- }
- /**
- * @End time
- */
- functionproEndTime() {
- global$startTime,$set;
- $mtime2=explode(" ", microtime());
- $endtime=$mtime2[1] +$mtime2[0];
- $totaltime= ($endtime-$startTime);
- $totaltime= number_format($totaltime, 7);
- echo"<br>process time: ".$totaltime;
- }
- // 程序調用開始記時
- proStartTime();
- sleep(1); // sleep() 延時代碼執行若干秒
- proEndTime();// 程序在每一段所消耗的執行時間
- sleep(2);
- proEndTime();
- sleep(3);
- proEndTime();
- /************************************************* 華麗的分割線 **************************************************/
- // 實例2
- //Vevb.com
- $t1= microtime(true);
- sleep(3);
- $t2= microtime(true);
- echo'程序耗時'.round($t2-$t1,3).'秒'
- ?>
新聞熱點
疑難解答