PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Script Execution Time
Your Ad Here

Script Execution Time


Script Execution TimeYou may have seen this feature of some sites around the net. Time execution basically means you can see how long it took for the page to load. This can be helpful when programming, or whenever.

An example of this script:

Script Execution Time: 0.001 seconds

Lets say you have a basic HTML page:

  
    
        
    
    
        Just some text Just some text Just some text
        Just some text Just some text Just some text
        Just some text Just some text Just some text
        Just some text Just some text Just some text
        Just some text Just some text Just some text
    
    

So we'll add the time execution script here.

The basic logic behind this is we need to see at what time the script started, and when it ended. After that we just need to find the difference between those two numbers.

Instead of using the common mktime() function to get the current timestamp, we'll use the following function to get the micro time. This is because usual execution times are much less then 1 second, so we'll need to use this for more accurate times.

The function we will be using to get the current micro time is this:

<?php
// Function to calculate script execution time.
{
    list ($msec, $sec) = explode(' ', microtime());
    $microtime = (float)$msec + (float)$sec;
    return $microtime;
}
?>

So once we have that, we'll need to implement this within our HTML page.

<?php

// Function to calculate script execution time.
function microtime_float ()
{
    list ($msec, $sec) = explode(' ', microtime());
    $microtime = (float)$msec + (float)$sec;
    return $microtime;
}

// Get starting time.
$start = microtime_float();

?>

        Just some text Just some text Just some text
        Just some text Just some text Just some text
        Just some text Just some text Just some text
        Just some text Just some text Just some text
        Just some text Just some text Just some text

<?php

$end = microtime_float();

// Print results.
echo 'Script Execution Time: ' . round($end - $start, 3) . ' seconds';   

?>     

That's about it, now you can see how long it takes to load the page.

NOTE you can change the accuracy of the time by changing the last parameter in the round() function. We use 3 [which means 3 numbers after the dot] but you can change that as you wish.



Author's URL: SweDesignz.com
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Script Execution Time"

Only registered users can write comment

No comments yet...