CODE: Down And Dirty Page Page Timing In PHP

March 30th, 2004
Blogging News

There are times when you’re working on a project and you need to test a page’s load
overhead. For instance, if you have a lot of database calls or are using some crazy regex
kung fu. These two little snippets of PHP code will give you a start to finish execution time
of the page you’re loading up. Put the first one at the very top and the second one at the
very bottom and load it up. When the page is finished rendering, just view source and you
can see the execution time in the HTML comment. You can remove the comment if you
want it to echo straight to your page. If you leave it in the comments, you can deploy this
in a production page and monitor the load times whenever you feel like it.

This goes at the top:

ereg(“(\.[0-9]+) ([0-9]+)”,microtime(),$time);
$starttime = doubleval($time[1])+doubleval($time[2]);

This goes at the end:

ereg(“(\.[0-9]*) ([0-9]*)”,microtime(),$time);
$runtime = number_format($endtime-$starttime,4).” Seconds”;
echo “<!– [$runtime] –>”;

There ya have it! [Jason]

…excerpt from:




  1. Matt (64 comments.) says:

    That’s actually a pretty bad way to do it. ereg? Useless. Check out timer_start and timer_stop in WP.

  2. Desi Quintans (1 comments.) says:

    Why not get assign the microtime at the start of a script to a temporary variable, then subtract it from the microtime at the end of the script? It’s much cleaner.

Obviously Powered by WordPress. © 2003-2013

page counter