Measure PHP page loading time

Measure page loading times with PHPA important part of the users experience, when visiting a website, is the page loading time.

However loading times has been even more important to focus on after the latest updates of the Google ranging algorithm, where slow loading sites will get a penalty, and fast loading sites will get a bonus.

In this post we’ll create a PHP script that can be used to measure the loading time of  a PHP page.

Is my site slow?

If you’re using Google Webmaster tools, you can see the loading times of your site, experienced by Google users, and you can also benchmark your site against other sites.

Google webmaster tools can thereby be very useful to get a high level feeling if our site is having problems with slow loading times.

Google sitespeed graph seo

If your site is slower than the average, there a risk that Google is penalizing your site in the search results, and that your users are getting a bad user experience. You should therefore start considering how reduce page loading tomes on your site.

Even though Google webmaster Tools gives valuable data about page loading times, you need a different tool with real time data when you’re starting to optimize your pages.

PHP code for page load time measuring

The PHP script for page load measuring is very simple. You place a piece of code at the very top of your page, and another piece of code at the very button of the page.

Each piece of code takes a timestamp, and the difference in time between the top timestamp and bottom timestamp is the time used to create the page.

This architecture makes this code very efficient to measure the performance of the PHP and MySQL part of the page.

If you want to optimize elements like images and CSS as well, you should include a tool like  Yslow in your optimization.

<?php
// Insert this block of code at the very top of your page:
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$top = $time;

// Place this part at the very end of your page
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$bottom = $time;

$loadtime = ($bottom-$top);
echo ("This page generated in $loadtime seconds");
?>

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

Comments are closed.