go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

Measuring PHP page load time

Sunday, 27th November, 2011  /  PHP Tutorials  / HTML / CSS Tutorials  /  55 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.
Share on:

55 Comments to "Measuring PHP page load time"

Anders Borg

Anders Borg / March 2, 2016 at 13:19 pm

I use this method to collect timing throughout scripts, e.g. the delta time for each MySQL query etc. At the end of the script I will then render a table of results in CSV format (so I can copy it into Excel etc) with labels, so I know what caused what. Of course only on my password-protected test installation.

What I've noted is that there's a lot of overhead outside of the script itself. A concrete example (accumulated and delta times in ms):

0.003;0.000;;Start
10.519;10.516;;sqlquery('SELECT * FROM customers WHERE id_customers='x' LIMIT 1') START
10.737;0.218;;sqlquery END
10.900;0.163;;sqlquery('SELECT * FROM plans WHERE id_plans='x' LIMIT 1') START
11.050;0.150;;sqlquery END
11.458;0.408;;End

Chrome Inspect reports a roundtrip of 30-40 ms.

I use Apache, that is not the fastest server, so it will contribute some of this, but what else do you think contributes the most to the total time?

Also, I noted that if I leave a script be for an hour and then request it again it will take much more time. Up to a second. Why is that? Is the script re-compiled/cached after at time? If so why? There's no shortage of storage. Does it cache in RAM?

I know, the answer to many of the above questions could be "It depends on your configuration".

What I have is a recently set up 1 CPU, 1 GB RAM installation on Digital Ocean using the LAMP 1-click and of course with OPcache active, which might narrow it down a bit.

Hafiz

Hafiz / December 31, 2015 at 03:59 am

hello how can i change position? i want to right place and how can i change the colour font? thx mate..

NeoFuture

NeoFuture / September 28, 2015 at 23:36 pm

2 line version


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

// Code //

<?
echo('<div>Page generated in '.round((explode(' ', microtime())[0] + explode(' ', microtime())[1]) - $start, 4).' seconds.</div>');
?>

Is no quicker so casting and other people complain about above is not a speed issue

JyothiPrasad

JyothiPrasad / July 8, 2015 at 12:22 pm

Works Great! Thanks!

umair

umair / March 12, 2014 at 14:31 pm

i make website speed calculator but no idea please help

deepak

deepak / December 12, 2013 at 11:19 am

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

muhammad muzammil

muhammad muzammil / September 28, 2017 at 04:30 am

I also want that code if you find it from anywhare please also share that with me..

John

John / September 24, 2013 at 14:56 pm

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

Alastair / October 16, 2013 at 21:03 pm

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

Tom / May 7, 2013 at 20:38 pm

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

Yug / April 17, 2013 at 17:00 pm

You mean the time now since 1980 ?

Alastair

Alastair / April 17, 2013 at 17:27 pm

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...

Alastair

Alastair / April 17, 2013 at 09:42 am

That "insane" value would be the current time (not the amount of time taken).

Add your comment

Captcha

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

    Log in to your account to post your comments. If you still haven't joined our community yet, 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

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts