<?php
/* DebugCodeTimer class. Include this at head of script that needs timed.
USAGE

To run more the timing loop more then once, and calculate fastest, slowest and
average execution times, simply specify 2 or more $timing_loops.
*/

class DebugCodeTimer {
	
	var $fStartTime;
	var $fFinishTime;
	var $Set = false;

	function DebugCodeTimer() {
	}

	function Start() {
		list($usec, $sec) = explode(" ", microtime());
		$this->fStartTime = ((float)$usec + (float)$sec);
		$this->Set = true;
	}

	function Finish() {
		list($usec, $sec) = explode(" ", microtime());
		$this->fFinishTime = ((float)$usec + (float)$sec);
	}
	
	function Show($comment = "Script execution") {
		//print "<style type=\"text/css\">.report_timing {font:8pt Verdana}</style>";
		$timing[] = $this->fFinishTime - $this->fStartTime;
		if(count($timing) == 1) {
			print $comment . "&nbsp;" . sprintf('%01.6f', ($timing[0] * 1000)) . " msecs";
		} else {
			sort($timing);
			$ave = array_sum($timing)/count($timing);
			print "Script execution over " . count($timing) . " loops averages " . $ave . " seconds<br>
			Fastest: " . $timing[0] . " seconds<br>Slowest: " . $timing[count($timing)-1] . " seconds";
		}
	}

	function Set() {
		return $this->Set;
	}
}