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

ATLChris picture ATLChris · Dec 10, 2010 · Viewed 25.4k times · Source

What I have in the header:

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;

What I have in the footer:

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo 'Page generated in ' . $total_time . ' seconds.';

Output: Page generated in 1292008977.54 seconds.

Can someone please help me figure out why the result is not right?? I am using PHP5.

Answer

GhostInTheSecureShell picture GhostInTheSecureShell · Mar 8, 2013

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.