Measuring time to execute PHP script

If I’m ever wondering how long my PHP scripts take to execute, I add this code to the start of my pages:

<?php
function getTime()
{
$a = explode (' ',microtime());
return(double) $a[0] + $a[1];
}
$Start = getTime();
?>

Then I add this to the end of my script:

<?php
$End = getTime();
echo "Time taken = ".number_format(($End - $Start),2)." secs";
?>

Sometimes it’s good to get hard figures on how long your script takes to execute, particularly with complex MySQL queries. If your script is particularly tardy and you want to find out where the blockage occurs, you could also try moving the $Start or $End lines to different parts of the script to measure the execution speed of each section.

Leave a Reply

Your email address will not be published. Required fields are marked *