Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

How to make "Page Load Time" stopwatch on PHP

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Intermediate Networking, Programming and Graphics - Tutorials Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2008-Dec-05
Networking, Programming and Graphics - Tutorials
Visited: 4454 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

All of us have seen "Page Load Time: 0.327 sec." at the bottom / footer in some web sites. This is not required for the right functioning of the web site, but gives good idea what performance we can expect from this web site and web server when the traffic grow up.

To measure the time need to process / load on web page, we will function to get absolute time on start of PHP parsing and absolute time when this parsing will end. Next we will calculate the difference between end time and start time and the result is time need for processing the page.

To measure this time we will use PHP microtime() function.
<?php
$startime
=getmicrotime(); 

// here is actual PHP code of the webpage
// ...................
// ...................
// ...................
// end of actual code

$endtime=getmicrotime(); 
echo 
"Page Load Time: ".($endtime-$startime)."seconds.";
?>
PHP microtime() function returns the current Unix timestamp in microseconds. The function returns the string "msec sec" where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds.

We will define PHP function which parse microtime() result and return complete float result. This result later easily can be used to make difference between end and start time.
<?php
function getmicrotime() { 
    
$temparray split(" ",microtime()); 
    
$returntime $temparray[0] + $temparray[1]; 
    return 
$returntime
}
?>
And now this is complete PHP web page with sample code doing something which can measured and displayed after "Page Load Time:".

You can try changing the size of array, the action with array within while() operator or just to run the script on different web-servers to check how performance reflect on script execution time.

Also you can see other PHP related tutorials here.
<?php
function getmicrotime() { 
    
$temparray split(" ",microtime()); 
    
$returntime $temparray[0] + $temparray[1]; 
    return 
$returntime
}

$startime=getmicrotime(); 

// here is actual PHP code of the webpage
for ($i=0count($array)<100$i=rand(1,100)) {
    
$array[]=$i*$i*pi();
}
sort($array);
while(list(
$key,$value)=each($array)) {
    echo 
$key." : ".$value;
}
// end of actual code

$endtime=getmicrotime(); 
echo 
"Page Load Time: ".($endtime-$startime)."seconds.";
?>
Rate this tutorial:                    
Post Comment


    • posted on 2010-Jan-19 | 02:36:49 AM
      tytry

    • posted on 2010-Jan-19 | 06:59:42 AM
      Very elegant code!
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: