MIKESTOWE.COM

you are here

Simple Way to Get Page Rendering Time

Here is a super simple way to get the time it takes to render the page (well, almost). To start, you simply include the class, and then initiate it as a new object attached to a variable. Voila that’s it. The only thing you have to make sure of is that you do not unset that variable, otherwise your rendering time will be messed up.

You can use the __destruct function in the class to print out the rendered page time (as shown), or to store it to a database, etc… The sky’s the limit.

<?php
class renderTimer {

    function 
__construct() {
        
$this->start microtime(true);
    }
    
    
    function 
__destruct() {
        
$time number_format((microtime(true)  - $this->start),5);
        echo 
"Rendered in $time seconds";
    }
    
}
?>



<?php
// EXAMPLE USAGE
$renderTimer = new renderTimer// we need to connect it to an usused variable so that it stays until the page is done being rendered.  When PHP deletes the variable, it will likewise destruct the class.

echo 'now we are testing this... but I need to sleep...';
sleep(2);
echo 
'<br /><br />';
echo 
'oh wow, time to wake up...';
echo 
'<br /><br />and out the door I go...<br /><br />';
?>
Share this Page:
Facebook Twitter Linkedin Reddit Tumblr Email

Leave a Reply

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