Show load time on page

sark9012 picture sark9012 · Aug 10, 2014 · Viewed 23.5k times · Source

I want to show the users how long the page takes to fully load in the footer of my website.

How do I go about doing this? I assume there is a function that can be used for this?

Not sure what language this type of feature is developed in?

Any help would be appreciated, thanks.

Answer

Rahul Tripathi picture Rahul Tripathi · Aug 10, 2014

You may try like this:

$starttime = microtime(true); // Top of page
// Code
$endtime = microtime(true); // Bottom of page

printf("Page loaded in %f seconds", $endtime - $starttime );

As commented by Ed Heal you need to use JavaScript as network/proxy/routes need to be factored in.

Also you may try this approach as well:

From the source

Put the following code at the very top of your PHP page (if you measure the time needed for particular part of the code put this right before that PHP code part)

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

The following code has to be put at the very end of the web page (or the end of the PHP code part)

<?php
$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.';
?>