"tracking the script execution time in php" Code Answer

3

On unixoid systems (and in php 7+ on Windows as well), you can use getrusage, like:

// Script start
$rustart = getrusage();

// Code ...

// Script end
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 "This process used " . rutime($ru, $rustart, "utime") .
    " ms for its computationsn";
echo "It spent " . rutime($ru, $rustart, "stime") .
    " ms in system callsn";

Note that you don't need to calculate a difference if you are spawning a php instance for every test.

By zerzer on August 15 2022

Answers related to “tracking the script execution time in php”

Only authorized users can answer the search term. Please sign in first, or register a free account.