ใน PHP เวอร์ชัน 7+ สามารถใช้ฟังก์ชัน getrusage ได้ ด้านล่างนี้คือตัวอย่างโค้ดการสาธิต -
ตัวอย่าง
//beginning of the script
$exec_start = getrusage();
//other code functionalities
//end of the 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"; หมายเหตุ − ไม่จำเป็นต้องคำนวณความแตกต่างของเวลาหากมีการสร้างอินสแตนซ์ php สำหรับการทดสอบทุกครั้ง
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
The process used 1.563896 ms for the computations Spent 0.345628 ms during system calls