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
ayush

Thanks Very Use Full
Mahesh

this is a very useful script. Thanks much.
Murph

I don't think this is working for me! Haha

Page generated in 1233606266.55 seconds.
April

If you were really interested in pursuing this further, you could create a small statistics table that you could insert into after the page load statistics were analyzed. You could then figure out the max load time, the time of day of longer load times, and average load time. When you made changes to code, or system upgrades, you could average out the change to load time averages. If you did the 'insert' to the tiny table *after* you calculated the page load time, the insert would not affect the load time stats.
Adam

I suppose the other question is... how do you know what a good average load time is for your page considering the following variables...

If it says "Page loaded in 0.03 seconds.."

-> is that fast?, is that too slow? and thus, do I NEED to spend time thinking about making it faster?
-> is it just because the server I'm on is already over loaded with other things?
-> is it a really good time considering the things the script does?

I guess to summarise, the question is... "The page has loaded in 0.025 seconds".. what am I to compare that to in order to understand if it's fast or slow...




HB

Thanks, I was trying with time() and not microtime(), couldn't get distinct measurements. This helped a lot.
Alex Panait

Hello,

I am trying to display on my site load times for other sites. Do you happen to know how this can be done ?

Please let me know, I am trying to do this for a few days and I have no idea,
Mohammad Samy

Thank you very much,I like these smart solutions and i wait for more, thank you.
maxreading.com

Thanks !
shan

Thank you very much for this little script. It helped me figure out which part of one of my scripts was running slow.
 
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