Computer >> Computer tutorials >  >> Programming >> PHP

How to find PHP execution time?


In PHP version 7+, the getrusage function can be used. Below is a sample code demonstration −

Example

//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";

Note − There is no need to calculate the time difference if a php instance is spawned for every test.

Output

This will produce the following output −

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