PHP benchmark class

< ?php
/**
 * benchmark
 * @access public
 * @var $timing_start_times float
 * @var $timing_stop_times float
 */
class benchmark{

	public $timing_start_times;
	public $timing_stop_times;


	/**
	* benchmark::timer_start()
	* @access public
	* @param string $name
	* @return int
	*/
	public function timer_start($name = 'default'){
		$this->timing_start_times[$name] = explode(' ', microtime());
	}

	/**
	* benchmark::timer_stop()
	* @access public
	* @param string $name
	* @return
	*/
	public function timer_stop($name = 'default'){
		$this->timing_stop_times[$name] = explode(' ', microtime());
	}

	/**
	* benchmark::timer_result()
	* @access public
	* @param string $name
	* @return int
	*/
	public function timer_result($name = 'default'){
		if (!isset($this->timing_start_times[$name])) {
        	return 0;
    	}
    	if (!isset($this->timing_stop_times[$name])) {
        	$stop_time = explode(' ', microtime());
    	}
    	else
		{
        	$stop_time = $this->timing_stop_times[$name];
    	}
	    // do the big numbers first so the small ones aren't lost
	    $current = $stop_time[1] - $this->timing_start_times[$name][1];
	    $current += $stop_time[0] - $this->timing_start_times[$name][0];
	    return $current;
	}

}
?>

example code

< ?php
require_once "benchmark.class.php";
$bm = new benchmark();
?>

Test Inline Tags vs echo

< ?php $bm->timer_start('echo'); ?> < ?php for ($i=0; $i<1000; $i++) { echo $i."
"; } ?> < ?php $bm->timer_stop('echo'); ?>

< ?php $bm->timer_start(str); ?> < ?php $str = ''; for ($i=0; $i<1000; $i++) { $str .= $i."
"; } echo $str; ?> < ?php $bm->timer_stop(str); ?>

< ?php $bm->timer_start(inline); ?> < ?php for ($i=0; $i<1000; $i++) { ?> 123
< ?php } ?> < ?php $bm->timer_stop(inline); ?>


Result

echo - < ?php echo $bm->timer_result('echo'); ?>

str - < ?php echo $bm->timer_result(str); ?>

inline - < ?php echo $bm->timer_result(inline); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *