PHPjabbers - tools for webmasters
shopping cart (0 item)
HOME
PRODUCTS
PHP TUTORIALS
PHP FORUM
ABOUT US
CONTACT US
Our products

Measuring PHP page load time

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)


$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.'."\n";
?>

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.

COMMENTS
Davy

This is something anyone can come up with. And it only calculates the time a server takes to construct a page. NOT the time it takes for a browser to load the page, from to moment it requested the page!
Clowerweb

Thanks! Works great!
Jacklynn

There's nhtiong like the relief of finding what you're looking for.
Eddie

Very very helpful, mate :)

Thanks a lot!
SlipShod

Good little piece of info. For those of you having trouble understanding what is going on...

You are simulating a stopwatch, when your code starts you capture the current time, then after your code runs you capture the time again and subtract the start time to get elapsed time: ($time_elapsed = $stopTime - $startTime). You cannot output the elapsed time BEFORE the code has completed, unless you only want to time a specific piece of code.

microtime() returns the current time in sec.milliseconds.

Also, <? ?> style tags are called short tags while <?php ?> is referred to as long tags. They ARE supported in PHP5+. Someone stated earlier that they were not. You have to change a setting on your webserver to allow or dissallow short style tags. See your webserver documentation.
surya

its working good...
if i want to know loading time of other websites using php coding
Code

function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
iptables

i use thdev 2009(Final)
Page generated in '1275657082.0379' seconds.
these is what i get but my page is loading in 1 second
David Rivet

My time is being displayed top left above my banner. How can I get it to be displayed center of page as the very last thing on the page site?

http://acadiasgarden.net


Best Regards
David 'stormy' Rivet
David Rivet

My time is being displayed top left above my banner. How can I get it to be displayed center of page as the very last thing on the page site?

http://acadiasgarden.net


Best Regards
David 'stormy' Rivet
 
POST A COMMENT
Your name:
Your email: (email address will not be posted on the website)
Comment:
Verification code:
type characters on the image in the text box