Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Cách tìm thời gian thực thi PHP


Trong PHP 7+, hàm getrusage có thể dùng. Mã mẫu:

Ví dụ

// đầu script 
$exec_start = getrusage(); 
// code chức năng khác 
// cuối script 
function rutime($ru, $rus, $index) { 
    return ($ru["ru_$index.tv_sec"] * 1000 + intval($ru["ru_$index.tv_usec"] / 1000)) 
    - ($rus["ru_$index.tv_sec"] * 1000 + intval($rus["ru_$index.tv_usec"] / 1000)); 
} 
$ru = getrusage(); 
echo "The process used " . rutime($ru, $exec_start, "utime") . 
    " ms for the computations\n"; 
echo "Spent " . rutime($ru, $exec_start, "stime") . 
    " ms during system calls\n";

Lưu ý - Không cần tính chênh lệch thời gian nếu spawn instance PHP cho mỗi test.

Kết quả

The process used 1.563896 ms for the computations Spent 0.345628 ms during system calls