Code snippet: Getting PHP page generation time

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: 8.5/10 (8 votes cast)
Code snippet: Getting PHP page generation time, 8.5 out of 10 based on 8 ratings

About Armino

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.