PHP Execution Time Class

The Basics

Ever wanted to benchmark certain code segments, actions, or the exeuction of your entire script in PHP? Knowing the execution time of different parts of your code is very valuable information. You can assess whether further code optimizaion need be done, or whether parts of the script should be on different pages. You can gauge the file access times when you query your database or open a local or remote file inside of a script. Benchmarking code constructs is the basis of my PHP Benchmarking section, where I experiment to find out which constructs of PHP code are faster than others.

The Script

This script is a simple object-oriented, PHP4+ compatible class. Being object oriented allows multiple TimeCounter objects to exist on one page, or on different pages throughout the site. While you can copy this class into each page that you want to use it on, the best way is to include it in its own file at the top of your page as shown below.

<?php

include('time_counter_script.php');

?>

The TimeCounter() class is shown below:

<?php
class TimeCounter
{
	var $startTime;
	var $endTime;
	
	function TimeCounter()
	{
		$this->startTime=0;
		$this->endTime=0;
	}
	function getTimestamp()
	{
		$timeofday = gettimeofday();
		//RETRIEVE SECONDS AND MICROSECONDS (ONE MILLIONTH OF A SECOND)
		//CONVERT MICROSECONDS TO SECONDS AND ADD TO RETRIEVED SECONDS
		//MULTIPLY BY 1000 TO GET MILLISECONDS
		 return 1000*($timeofday['sec'] + ($timeofday['usec'] / 1000000));
	}
	function startCounter()
	{
		$this->startTime=$this->getTimestamp();
	}
	function stopCounter()
	{
		$this->endTime=$this->getTimestamp();
	}
	function getElapsedTime()
	{
		//RETURN DIFFERECE IN MILLISECONDS
		return number_format(($this->endTime)-($this->startTime), 2);
	}
}
?>

Usage

Using this class is simple. After initializing a TimeCounter object, call the startCounter() function on it. Then execute the code you want to be timed. When you are done, call the stopCounter() function on your TimeCounter object. To output the result, echo the results of calling the getElapsedTime() function on your TimeCounter object.

<?php

//INCLUDE CLASS FROM OTHER FILE
include('time_counter_script.php');

//INITIALIZE COUNTER CLASS
$counter=new TimeCounter();

//START COUNTER
$counter->startCounter();

//DO SOMETHING WORTH TIMING

//STOP COUNTER
$counter->stopCounter();

//OUTPUT RESULT
echo $counter->getElapsedTime();
?>

Script Notes

The time will be returned in milliseconds, which are one thousadth of a second, and will be rounded to 2 decimal points. Because this script is object oriented, you can declare as many TimeCounter() objects as you want and they can all be used concurrently.

Extending this Example

I use this class often in my web applications. Usually, I display the execution time of the entire script in the footer of each page. This allows me to quickly and easily determine any potential bottlenecks as I test the application. And, more importantly, as the application ages, I can come back and test it again and again without having to change any code.