- Colection of 65 PHP scripts for $4.29 each
Measuring PHP page load time
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.
55 Comments to "Measuring PHP page load time"
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.
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
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
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 / 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 / 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 / April 17, 2013 at 17:00 pm
You mean the time now since 1980 ?
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...