Tuesday, 23 January 2018

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);
}

// 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.";

0 comments:

Post a Comment