Related Books

Performance Timer Class

score: 1  posted by: mkaz  in: php  tags: timer, performance, benchmarking
Use this class to measure how long it takes for a script or function to execute
# Performance Timer Class
# Modified from: http://www.phpfreaks.com/tutorials/121/0.php

# Usage:
# $timer = new mkaz_timer();
# /* do something that you want to time */
# $timer->stop()

class mkaz_timer {
var $start;

// constructor
function mkaz_timer() {
$this->start = microtime();
return true;
}

function stop($decimals=2) {
$end = explode(" ", microtime());

$endtime = (float)$end[1] + (float)$end[0];
$starttime = explode (" ", $this->start);
$starttime = (float)$starttime[1] + (float)$starttime[0];

$decimals = intval($decimals);
if ($decimals > 8) { $decimals = 8; }
if ($decimals < 0) { $decimals = 0; }
$secs = number_format($endtime - $starttime, $decimals);

if ($secs > 60) {
$mins = floor($secs / 60);
$secs = $secs - $mins*60;
}

if ($mins > 60) {
$hrs = floor($hrs / 60);
$mins = $mins - $hrs*60;
}

$str = "";
if (!empty($hrs)) { $str .= " $hrs hrs "; }
if (!empty($mins)) { $str .= " $mins mins "; }
if (!empty($secs)) { $str .= " $secs secs "; }
echo "Process $str \n";

}
}

Comments (login to comment)