Web Development

Displaying Load Time with PHP

Displaying Page Load Times with PHP

When developing PHP-driven websites its often very useful to know how long your pages took to process.  This not only gives you some idea of the efficiency of your websites and of the server running them, but may help you diagnose problems, benchmark code corrections/additions, etc.  The best way to do this is program PHP to read the system time at the beginning of the page, scan it again near the end, and then work out the difference between the values.  To get us started, just add the following basic code near the top of your page, before all the main content:

<?php
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
?>

It may look complex, but the above code is actually dead simple.  Microtime is a function that returns a value giving the current time in seconds and milliseconds from the UNIX Epoch (internationally set as 0:00:00 January 1, 1970 GMT).   Explode then splits this value into its two component parts (seconds and milliseconds), and then inserts both values separately into an array.   With those values determined, of course, we can go on to insert the following code near the end of the webpage, after all the main code:

<?php
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime – $starttime;
$totaltime = round($totaltime,5);
echo "This page loaded in $totaltime seconds.";
?>

I’m pretty sure you can guess what’s going on here.  The microtime is determined again and inserted into the endtime array.  We then delete the starttime array from it and come up with a brand new value, totaltime (which is the value we’ve wanted all along).  The number is then rounded to 5 significant figures, and output by the ECHO command.  If you’ve entered everything correctly, you should end up with a line of text saying something like this:

Page generated in 0.27283 seconds

And that, as they say, is that!   As you can probably guess, the lower this value the better the performance. Don’t get too obsessed with these figures, though, because they also rely heavily on server load levels, mySQL commands, etc.  Just look at everything with a scientifically objective eye, and all will be fine… :)

About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. Displaying Alternate Page Area in an IFRAME
  2. Displaying Current Date with JavaScript and Basic Math Calculations
  3. Eye On Performance: A Load Of Stress
  4. Basic JavaScript Date and Time Functions
  5. Using Control Structures and Foreach Loops in PHP
  6. Creating and Accessing MySQL Data with PHP
  • http://www.facebook.com/profile.php?id=100000898430631 Thiery Fc

    First Comment -
    Seriously though, nice tutorial, helpful for the newbies like myself.