#native_company# #native_desc#
#native_cta#

Find out how fast your page generates

By Josh Petrovich
on May 24, 2003

Version: 1.2

Type: Function

Category: Other

License: GNU General Public License

Description: Whenever you think you have a time-consuming script, use these functions to find out about how long time your page needs to be generated. Just call the first function first in the script, and the last function last, and they will report how much time elapsed in between. You might use this to try out if you can optimize your script to make it faster.

<?
	$timekeeper = array();
	$timereport = array();


	function start_track($item, $timekeeper)
	{
		$timeStart=gettimeofday();
		$timeStart_uS=$timeStart["usec"];
		$timeStart_S=$timeStart["sec"];
		$item1 = $item . "_usec";
		$item2 = $item . "_sec";
		$timekeeper[$item1] = $timeStart_uS;
		$timekeeper[$item2] = $timeStart_S;
		return $timekeeper;
	}

	function end_track($item, $timekeeper, $timereport)
	{
		$timeEnd=gettimeofday();
		$timeEnd_uS=$timeEnd["usec"];
		$timeEnd_S=$timeEnd["sec"];
		$item1 = $item . "_usec";
		$item2 = $item . "_sec";
		$start_uS = $timekeeper[$item1];
		$start_S = $timekeeper[$item2];
		$ExecTime_S = ($timeEnd_S+($timeEnd_uS/1000000))-($start_S+($start_uS/1000000));
		$timereport[$item] = $ExecTime_S;
		return $timereport;
	}

	function time_report($timereport)
	{
		while(list($key, $time) = each($timereport))
		{
			print "$key - $time sec.<br>n";
		}
	}

        // use these lines to perform the function calls. Make sure that you
        // use the name name for the start and stop functions or it won't work
        // make as many calls as you want wherever you want then use time_report
        // to list all the different times

	$timekeeper = start_track("stringofyourchoice", $timekeeper);
	$timereport = end_track("stringofyourchoice", $timekeeper, $timereport);

        // when all is said and done, call this after last end_track has been called

	time_report($timereport);
?>