在寫PHP項目的時候,發現速度很慢,于是查看一下是哪里影響的。
<?php // 實例1 /** * @start time */ function proStartTime() { global $startTime; $mtime1 = explode(" ", microtime()); $startTime = $mtime1[1] + $mtime1[0]; } /** * @End time */ function proEndTime() { global $startTime,$set; $mtime2 = explode(" ", microtime()); $endtime = $mtime2[1] + $mtime2[0]; $totaltime = ($endtime - $startTime); $totaltime = number_format($totaltime, 7); echo "process time: ".$totaltime."\r\n"; } // 程序調用開始記時 proStartTime(); sleep(1); // sleep() 延時代碼執行若干秒 proEndTime(); // 程序在每一段所消耗的執行時間 sleep(2); proEndTime(); sleep(3); proEndTime(); /************************************************* 華麗的分割線 **************************************************/ // 實例2 $t1 = microtime(true); sleep(3); $t2 = microtime(true); echo '程序耗時'.round($t2-$t1,3).'秒'; ?>
轉載請注明出處 AE博客|墨淵 ? PHP檢測每一段代碼執行時間
發表評論