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:
<?php
ereg(“(\.[0-9]+) ([0-9]+)”,microtime(),$time);
$starttime = doubleval($time[1])+doubleval($time[2]);
?>
This goes at the end:
<?php
ereg(“(\.[0-9]*) ([0-9]*)”,microtime(),$time);
$endtime=doubleval($time[1])+doubleval($time[2]);
$runtime = number_format($endtime-$starttime,4).” Seconds”;
echo “<!– [$runtime] –>”;
?>
There ya have it! [Jason]
…excerpt from: http://channels.lockergnome.com/web/backissues/20040324.phtml#20040324_2
That’s actually a pretty bad way to do it. ereg? Useless. Check out timer_start and timer_stop in WP.
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.