Redirect based on referrer or IP address »

Measuring PHP page load time Posted in PHP Tutorials, HTML/CSS Tutorials | 50 Comments
You probably wonder how much time is needed for your PHP page to load. It can be crucial for your web server if your php scripts load slow and take most of the CPU and RAM resources. It is also a good idea to measure parts of your PHP code which can cause delays such as for/while cycles, writing/reading to/from files or MySQL database.

Using microtime() PHP function you will know exactly how much time is needed for your PHP code to be executed. Follow the steps below to put the PHP code on your web page:

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


Now, when you access your web page you will know the time needed for it to execute. This is an example output of what you are going to see:
Page generated in 0.0031 seconds.

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

50 Replies to "Measuring PHP page load time"

umair March 12, 2014 at 2:31 pm | Reply

+2

i make website speed calculator but no idea please help
deepak December 12, 2013 at 11:19 am | Reply

+1

i need complete php source code to get load time of any website to enter the url but load time doesnot change after reloading a page again and again
John September 24, 2013 at 2:56 pm | Reply

+1

LOL this script increased the load time considerably!!!!


USE John Hornsby's CODE INSTEAD!!!!!! :

<?php $start_time = microtime(true); ?>

// Content of your page

This page was generated in <?php echo(number_format(microtime(true) - $start_time, 2)); ?> seconds.
Alastair October 16, 2013 at 9:03 pm

+5

Unless you're testing an otherwise empty script, I'd be seriously worried if some simple string operations are increasing your loading time considerably. Might be time to investigate whichever server you're using and/or PHP-implementation!
Tom May 7, 2013 at 8:38 pm | Reply

0

Is there a way to load the whole page then load the $total_time in the middle? Normally nothing will show because its empty due to the end of the script not being possessed

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

CONTENT

<?=$total_time?>

CONTENT

<?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.';
?>
Yug April 17, 2013 at 5:00 pm | Reply

+3

You mean the time now since 1980 ?
Alastair April 17, 2013 at 5:27 pm

+2

The (then-)current time as represented in Unix time: The number of seconds that have passed since 1970-01-01 (with no/GMT offset). Converting it into a more human-friendly format with
date('c',1366144357.63)
results in 2013-04-17T07:32:37+11:00 for me.

It seems like your problem is that you're just returning the value of $start instead of the difference between $start and $finish...
John Hornsby January 13, 2013 at 6:27 pm | Reply

+10

A simpl(est) piece of code could be:

<?php $start_time = microtime(true); ?>

// Content of your page

This page was generated in <?php echo(number_format(microtime(true) - $start_time, 2)); ?> seconds.
Calin Rusu November 26, 2012 at 3:47 am | Reply

+4

A simpler pice of code (without exploding the microtime) would be:


//verybeginning of your page
$start = microtime(true);
//very end of your page
$end = microtime(true);
print "Page generated in ".round(($end - $start), 4)." seconds";
YTEpisodes October 24, 2012 at 3:17 am | Reply

+3

It's possible to display the generated time at the top of the page, if your code/html is set up to generate code first, then spit out HTML. A basic coding practice I use in building sites is to 1) include my "common functions" file, run the code needed for somepage.php, include head.php (which also has its own code), display somepage.php with the generated variables filled in, then include foot.php.

The first set of code (the "start" marker) would be added at the top of my common functions file, since this's the first thing to get processed on any page. The stop marker would be added at the end of my head.php code, right before I start displaying the HTML. (Because in all files, I do code first, HTML output second.) Then I could add it at the top of the webpage. Some extra code might be processed in foot.php, but not enough to really make a difference.
najmul October 10, 2012 at 9:32 pm | Reply

0

How i can display the result on the top of my webpage?
Alastair May 8, 2013 at 6:57 am

0

echo "<div id='generation-time'>$total_time</div>";



#generation-time {
position: absolute;
top: 0px;
}
Johnny April 25, 2012 at 2:53 pm | Reply

+4

It says 'Page generated in 0.0001 seconds.' no matter how many pictures i have on my site.

How can i measure how long time it takes for all php, html & pictures to load?
Brian Wigginton December 15, 2012 at 2:14 am

+1

You can't do this from the server (using PHP).

Images are loaded in via your client's browsers. Use the developer tools for your browser of choice to see how fast everything loads in on your machine.


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~