找出效能瓶頸需先測量。PEAR Benchmark 套件提供 Benchmark_Timer 與 Benchmark_Iterate 兩類別,方便進行微基準測試。
<?php
require_once 'Benchmark/Iterate.php';
$bench = new Benchmark_Iterate();
$bench->run(10, 'myFunction', 'test');
$result = $bench->get();
echo "<pre>";
print_r($result);
echo "</pre>";
exit;
function myFunction($var) {
echo 'Hello ';
}
?>
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
Array
(
[1] => 0.000427
[2] => 0.000079
...
[mean] => 0.000108
[iterations] => 10
)
mean:平均執行時間。iterations:執行次數。Benchmark_Timer 可測量特定程式碼區塊的啟動/結束時間點。Benchmark_Profiler 可針對函數/方法呼叫進行分析,找出熱點程式碼。