Calculate Page Execution time in PHP

Share this Article :


For those people who want to give users that in how many seconds actually your web page loaded completely. This script will be useful for you. There are so many ways to do this ( As usual ) but the classic approach would be

Step 1 : make a note of  the time when your page get starts

Step 2: make a note of  the time  at the end of your total page execution

Step 3 : step 2 – step1, You will get your page execution time

In simple   Here is the function  which will do it for you .

You have  to   call the function at the  start  and   echo  it  at the  end.   simple !!!!

 
//  Function   to calculate  Page Execution
function  pageTime()
{
static $_pt;
    if($_pt == 0) $_pt = microtime(true);
    else return (string)(round(microtime(true)-$_pt ,3));
}

Here is the Usage

<?php
function  pageTime()
{
static $_pt;
    if($_pt == 0) $_pt = microtime(true);
    else return (string)(round(microtime(true)-$_pt ,3));
}
 
pageTime();
 
//  Include headers,  include files,  etc 
 
// start your  operations 
 
// Display your  output 
 
// You are done  almost  
 
echo "The Page  Executed  in  ". pageTime()." Seconds ";
?>

Thats it folks … Happy coding !!

Written by adrevol

{adrevol has written 91 posts on ITTreats.com . See all posts by }


Leave a Reply