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); ?>

PHP cache class

< ?php
/**
*
* @var $cache_file_name string file name
* @var $age int age in seconds
*/
class cache {

	public $cache_file_name;
	public $age;

	/**
	* cache::__construct()
	* @access public
	*/
	public function __construct(){
		$this->cache_start();
		register_shutdown_function(array($this, "cache_end"), "inside");
	}

	/**
	* cache::__descruct()
	* @access public
	* @return
	*/
	public function __descruct(){
		$this->cache_end();
	}

	/**
	* cache::cache_start()
	* @access public
	* @return
	*/
	public function cache_start(){

		global $cache_file_name, $age;
		$cache_file_name = 	$_SERVER["DOCUMENT_ROOT"].$_SERVER['REQUEST_URI'] . '_cache';

		if (empty($age)){
			$age = 600;
		}

		if(file_exists($cache_file_name)){
			if (filemtime($cache_file_name) + $age > time()) {
				readfile($cache_file_name);
				unset($cache_file_name);
				exit;
			}
		}
		ob_start();
	}

	/**
	* cache::cache_end()
	* @access public
	* @return
	*/
	public function cache_end()
	{
		global $cache_file_name;
		if (empty($cache_file_name)){
			return;
		}
		$str = ob_get_clean();
		echo $str;
		fwrite(fopen($cache_file_name . '_tmp', "w"), $str);
		rename($cache_file_name . '_tmp',$cache_file_name);
	}

}

?>

Posts Tagged class

Archives by Month: