Elapsed microtime counter

Counts the elapsed seconds when doing some process.

// mt_get: returns the current microtime
function mt_get(){
    global $mt_time;
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
 
// mt_start: starts the microtime counter
function mt_start(){
    global $mt_time; $mt_time = mt_get();
}
 
// mt_end: calculates the elapsed time
function mt_end($len=4){
    global $mt_time;
    $time_end = mt_get();
    return round($time_end - $mt_time, $len);
}
Snippet Details



// start timer:
mt_start();
 
 
// put a long operation or 
// something similar in here:
for ($x=0; $x < 5000; $x++){
    print ($x % 2) ? '<!-- foo -->' : '';
}
 
 
// calculate elapsed time
$time = mt_end();
 
 
print "<br/><br/>The page needed ".$time." seconds to load.";

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.