在 PHP 開發中,精準測量函數執行時間是效能優化的基礎。本文分享一個輕量級的 timeit() 函數,模仿 Python 的 timeit 模組設計,支援指定執行次數與任意可呼叫單元,回傳浮點數秒數,適合快速比較不同演算法或內建函數的效能差異。
/**
* 計算函數重複執行的總耗時
* @param int $count 執行次數(必須 > 0)
* @param string|callable $function 目標函數,支援 'func(arg1,arg2)' 字串或 callback
* @return float 總耗時(秒),失敗回傳 -1
*/
function timeit($count, $function) {
if ($count <= 0) {
echo "Error: count have to be more than zero";
return -1;
}
$nbargs = func_num_args();
if ($nbargs < 2) {
echo 'Error: No Function!';
echo "Usage:\n\ttimeit(count, 'function(param)')\n\te.g:timeit(100, 'function(0,2)')";
return -1;
}
// 解析函數名稱
$func = func_get_arg(1);
$func_name = current(explode('(', $func));
if (!function_exists($func_name)) {
echo 'Error: Unknown Function';
return -1;
}
// 動態構建並執行計時程式碼
$str_cmd = '';
$str_cmd .= '$start = microtime(true);';
$str_cmd .= 'for($i=0; $i<'.$count.'; $i++) '.$func.';';
$str_cmd .= '$end = microtime(true);';
$str_cmd .= 'return ($end - $start);';
return eval($str_cmd);
}
time()'sqrt_nd(5)'),也可傳入 ['Class','method'] 或匿名函數
// 自訂牛頓迭代法求平方根
function sqrt_nd($num) {
$value = $num;
while (abs($value * $value - $num) > 0.001) {
$value = ($value + $num / $value) / 2;
}
return $value;
}
// 執行 1000 次比較
print timeit(1000, 'sqrt_nd(5)'); // 0.028280019760132
print "\n";
print timeit(1000, 'sqrt(5)'); // 0.0041000843048096
內建 sqrt() 耗時 0.0041 秒,自訂牛頓法耗時 0.0283 秒,效能差距約 6.9 倍。這印證了內建函數經過 C 層優化、SIMD 指令集加速,遠優於純 PHP 實作。實務上除非有特殊精度或教學需求,優先使用內建函數。
PHP 官方文檔說明 microtime(get_as_float):
"微秒 秒" 字串(如 "0.123456 1700000000")get_as_float = true:直接回傳 float 秒數(如 1700000000.123456),適合直接相減計算差值
$start_time = microtime(true);
// 待測程式碼區塊
for ($i = 1; $i <= 1000; $i++) {
echo $i . '<br>';
}
$end_time = microtime(true);
echo '執行時間:' . ($end_time - $start_time) . ' 秒';
memory_get_peak_usage() 同時觀察記憶體佔用timeit() 使用 eval(),若 $function 來自使用者輸入極度危險,僅限內部測試環境使用gc_disable() 後測試,結束再 gc_enable()掌握微秒級計時技巧,能幫助開發者在演算法選型、瓶頸定位、重構驗證等場景做出數據驅動的決策。timeit() 雖簡單,卻是效能優化工具箱中實用的輕量利器。