Script Execution Time

Tutorial Description:
Learn how to add a time execution script to your pages.
You 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.
To refresh your memory on how time and dates on the internet work, please check out, Playing with Dates.
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.

