PHP Get Page Load Stats - How to measure php script execution / load time PHP Get Page Load Stats - How to measure php script execution / load time php php

PHP Get Page Load Stats - How to measure php script execution / load time


Seeing how this is the first result in Google I thought I'd share my solution to this problem. Put this at the top of your page:

$startScriptTime=microtime(TRUE);

And then put this code at the bottom of your page:

$endScriptTime=microtime(TRUE);$totalScriptTime=$endScriptTime-$startScriptTime;echo "\n\r".'<!-- Load time: '.number_format($totalScriptTime, 4).' seconds -->';

When you view source of a page you can see the load time in a comment on the last line of your HTML.


microtime() returns the current Unix timestamp with microseconds. i don't see any math there that does the conversion from microseconds to seconds.

microtime(true) returns the time as a float in seconds


You can use this simple function to avoid the variable scope issue:

<?phpfunction timer(){    static $start;    if (is_null($start))    {        $start = microtime(true);    }    else    {        $diff = round((microtime(true) - $start), 4);        $start = null;        return $diff;    }}timer();echo 'Page generated in ' . timer() . ' seconds.';