首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 程式加速探索:腳本執行速度測試與基準測試

     編輯:PHP 相關知識

前言

找出效能瓶頸需先測量。PEAR Benchmark 套件提供 Benchmark_Timer 與 Benchmark_Iterate 兩類別,方便進行微基準測試。

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:執行次數。

實務建議

  • 測試次數建議 ≥ 1000 次,減少系統干擾,獲得較穩定平均值。
  • 搭配 Benchmark_Timer 可測量特定程式碼區塊的啟動/結束時間點。
  • 測試環境應盡量接近生產環境(相同 PHP 版本、擴展、資料量)。

進階用法

Benchmark_Profiler 可針對函數/方法呼叫進行分析,找出熱點程式碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved