I took a little break from writing the main series, which is published here - about the user registration web application. Soon I hope I will take my time writing the next part, but until then here is simple class for profiling your applications:
< ?php class My_CodeProfile { protected static $startTimes; protected static $endTimes; protected static $durations; /** * Private constructor, so objects cannot be constructed from this class */ private function __construct() { } public static function start($counter = "def") { $counter = self::filterCounterName($counter); if ($counter === "") throw new Exception('Not valid counter name'); self::$startTimes[$counter] = microtime(); } public static function stop($counter = "def") { $counter = self::filterCounterName($counter); if ($counter === "") throw new Exception('Not valid counter name'); self::$endTimes[$counter] = microtime(); $aA = explode(' ',self::$startTimes[$counter].' '.self::$endTimes[$counter]); //seperate values and put parts in array if (!isset(self::$durations[$counter])) self::$durations[$counter] = 0; self::$durations[$counter] += (($aA[2]+$aA[3])-($aA[0]+$aA[1])); //calculate and store } public static function printTime($counter = "def") { $counter = self::filterCounterName($counter); if ($counter === "") throw new Exception('Not valid counter name'); if (!isset(self::$durations[$counter])) return false; if($counter != "def") return sprintf('Counter '.$counter.': %03.4f Seconds', self::$durations[$counter]); else return sprintf('%03.4f Seconds', self::$durations[$counter]); } public static function getTime($counter = "def") { $counter = self::filterCounterName($counter); if ($counter === "") throw new Exception('Not valid counter name'); if (!isset(self::$durations[$counter])) return false; return self::$durations[$counter]; } protected static function filterCounterName($counter) { $filter = new Zend_Filter_Alnum(); return $filter->filter($counter); } } ?>
The class uses only static methods and the usage is very simple:
My_CodeProfile::start() will start the timer, My_CodeProfile::stop() will stop it and
echo My_CodeProfile::printTime() will print on your screen something like “0.0712 Seconds”.
The counter can be started and stopped many times, for example:
for ($i=1; $i< =1000; $i++) { //some code here //..... //we want to profile only this part of the loop: My_CodeProfile::start(); // the code, which we want to profile // ..... My_CodeProfile::stop(); // other code here, which we are not interested in at this moment: // ...... } echo 'Execution time of the code, which we have profiled: ' . My_CodeProfile::printTime();
Another thing - we can have more timers at the same time - all static functions support single parameter ($counter), which is a string - the name of the counter. Its default value is ‘def’, so the only limitation is that we can’t use ‘def’ as custom name for our timer.
So now go and find those bottlenecks ![]()
And yes, I know there is PEAR Benchmark, but another shot (much simplified than the PEAR one) is still valuable to me.













viperx | 31-Jul-08 at 1:33 pm | Permalink
added ability for printTime to print transitional time (before stop() is invoked):