Calculate how long a page takes to load with a page timer

To measure the time of a page, query, or whatever you want to measure you need to set up a timer first and then check it.

You will need these two functions:

function TimerStart() {
    // Load this right at the start of the process you want to time
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

function TimerEnd($timerStart) {
    list($usec, $sec) = explode(" ", microtime());
    $timerEnd = ((float)$usec + (float)$sec);

    $timeTaken = $timerEnd - $timerStart;
    $timeTaken = number_format($timeTaken, 4, '.', '');

    return $timeTaken;
}

Here is an example of how you would use the timer:


///////////////////////////////////
// Calculate SHA1 hashes

$timer = TimerStart();

for ($i = 1; $i < = 100000; $i++) {
    $tmp = sha1($i);
}

$timerSha1 = TimerStop($timer);


///////////////////////////////////
// Calculate MD5 hashes

$timer = TimerStart();

for ($i = 1; $i <= 100000; $i++) {
    $tmp = md5($i);
}

$timerMD5 = TimerStop($timer);

echo "SHA1 generation time: <strong>$timerSha1 seconds<br />";
echo "MD5 generation time: <strong>$timerMD5</strong> seconds<br />";

/*
Output:

SHA1 generation time: 0.6527 seconds
MD5 generation time: 0.5495 seconds

*/
Leave a comment
Name
Email (optional)
Your email will only be used to show your Gravatar and to send you replies to this article (if you opt to receive them). You won't get ANY other messages at all!
Comment
No HTML allowed
Notify me via email
 
 
Share

Valid HTML 4.01 Strict