分析PHP腳本的最簡(jiǎn)單方法對(duì)PHP腳本進(jìn)行分析的最簡(jiǎn)單方法是什么?我很樂意在上面添加一些東西,顯示所有函數(shù)調(diào)用的轉(zhuǎn)儲(chǔ),以及它們花了多長(zhǎng)時(shí)間,但我也不介意在特定函數(shù)周圍放置一些東西。我試著用微時(shí)間職能:$then = microtime();myFunc();$now = microtime();echo sprintf("Elapsed: %f", $now-$then);但這有時(shí)會(huì)給我負(fù)面的結(jié)果。另外,在我的代碼中灑上這些也是很麻煩的。
3 回答

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
不需要擴(kuò)展,只需使用這兩個(gè)函數(shù)進(jìn)行簡(jiǎn)單的分析。
// Call this at each point of interest, passing a descriptive stringfunction prof_flag($str){ global $prof_timing, $prof_names; $prof_timing[] = microtime(true); $prof_names[] = $str;}// Call this when you're done and want to see the resultsfunction prof_print(){ global $prof_timing, $prof_names; $size = count($prof_timing); for($i=0;$i<$size - 1; $i++) { echo "<b>{$prof_names[$i]}</b><br>"; echo sprintf(" %f<br>", $prof_timing[$i+1]-$prof_timing[$i]); } echo "<b>{$prof_names[$size-1]}</b><br>";}
下面是一個(gè)示例,在每個(gè)檢查點(diǎn)使用描述調(diào)用prof_頻標(biāo)(),并在末尾調(diào)用prof_print():
prof_flag("Start"); include '../lib/database.php'; include '../lib/helper_func.php';prof_flag("Connect to DB"); connect_to_db();prof_flag("Perform query"); // Get all the data $select_query = "SELECT * FROM data_table"; $result = mysql_query($select_query);prof_flag("Retrieve data"); $rows = array(); $found_data=false; while($r = mysql_fetch_assoc($result)) { $found_data=true; $rows[] = $r; }prof_flag("Close DB"); mysql_close(); //close database connectionprof_flag("Done");prof_print();
輸出如下:
啟動(dòng)
0.004303
連接到DB
0.003518
執(zhí)行查詢
0.000308
檢索數(shù)據(jù)
0.000009
關(guān)閉DB
0.000049
已完成
- 3 回答
- 0 關(guān)注
- 403 瀏覽
添加回答
舉報(bào)
0/150
提交
取消