Load Time
A very common thing these days is to have on a dynamic page is a “load time” or “how long it took PHP to process your websites code”. This time is typically generated in fractions of seconds for most people’s websites, and is a statistic that PHP developers like to know so that they are lowering their system loads. In this tutorial, I’ll show you a very simple way to calculate this information, and I’ll also explain what it’s doing step by step.
First things first, we need a function to get the information on the time we want, so here it is for you, phpTime():
<?php
function phpTime (){
$tTime = explode( “ ”, microtime());
$dMsec = (double)$tTime[0];
$dSec = (double)$tTime[1];
return $dSec + $dMsec;
?>
What this function is doing for you is getting the information from microtime(), and splitting out the information we care about. The return value of microtime is something a tad strange, and it comes back as this:
0.76393100 1210608696
The second number in the set is the time, in seconds from the “Unix Epoch”, which is to say, 0:00:00 January 1st, 1970 GMT. And the first number is the microseconds portion of the time. So, by having both of these pieces of data, we can get a very acurate timestamp of how long it takes to perform an action.
Now that we have our function, we can go ahead and start setting up the necessary bits. Lets start with getting the start time, as obviously we need a frame of reference to compare the end time to. So, at the very beginning of your page (which by the way, MUST be a PHP page, if you’re trying to do this in HTML, forget it), before you have anything else, put the following bit of code:
<?php $startTime = phpTime(); ?>
What this does for us is saves a variable called $startTime which holds the time we began processing the page. And now that we have that, we can add the following piece of code to the very end of our website (or atleast, just before we wish to output this information, but please be aware, that the farther up the page, the less accurate the data returned):
<?php
$endTime = phpTime();
$diffTime = $endTime - $starTime;
?>
In this bit, we’ve just stored our final time in a variable called $endTime so that we now have our start and ending times stored in a manner we can play with them. We then subtract the value of $endTime from the value of $startTime and we get our result stored in $diffTime. This is now a floating point integer value of how much time it took us to complete the execution of our website, so we can display it in a manner like such:
<?php echo “Page Generated in ”.substr($diffTime,0,5).“ seconds.”; ?>
This will give us our time down to 0.000. The reason for using the substr() function is so that we don’t get this grossly long number (as the difference really has much more precision than that). If you’d like more detail, just increase the 5 in the function call to whatever you’d like, and it will go that far (assuming of course, that microtime() gave us that much precision back as a return).
And what will the output look like? Glad you asked, it’ll look like this:
Page Generated in 0.002 seconds.
Not to shabby if I do say so myself, and a very useful tool for when you get into more complex PHP scripting.
Thanks for reading, and enjoy.
No comments yet. Be the first.
Leave a reply