Code snippet: Getting PHP page generation time

code_snippet

This is a code snippet on how to get the execution time of a PHP script.
This is the first from the code snippet series.

<?php 

function GetMicroTime()
{
        list($microseconds, $seconds) = explode(" ", microtime());
        return ((float)$microseconds + (float)$seconds);
} 

//read the time at the beggining of the php file
$nStartTime = GetMicroTime(); 

/* Your code goes here
...
*/ 

//read the time at the end of the php file
$nEndTime = GetMicroTime();
$szGeneratedTime = "Page generated in " .
                    number_format(($nEndTime - $nStartTime), 4) .
                    " seconds.";
echo $szGeneratedTime;
?>

VN:F [1.9.11_1134]
Rating: 9.0/10 (4 votes cast)
Code snippet: Getting PHP page generation time, 9.0 out of 10 based on 4 ratings

About the Author

This website is about me, my experiences, my hobbies and my friends. If you are interested in my professional profile you can check it on LinkedIn.