Benchmark Your PHP Pages

Just came across this handy-dandy way to calculate the page generation time for any PHP page. At the top of the page, just put these four lines of PHP code:

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

Then, at the very bottom, add these 6 lines:

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
printf("<div align=\"center\">page generated in %f seconds </div>", $totaltime);

It will print out the total page generation time. Note if you want to get really anal about it, you could run the above lines by themselves, see how long it takes, and then subtract that from the total time of one of your regular pages to get the true generation time. But on my computer, the above takes 0.000018 seconds.

This entry was posted in PHP. Bookmark the permalink.

Leave a reply