<?php
    
/**
    * o------------------------------------------------------------------------------o
    * | This package is licensed under the Phpguru license 2008. A quick summary is  |
    * | that the code is free to use for non-commercial purposes. For commercial     |
    * | purposes of any kind there is a small license fee to pay. You can read more  |
    * | at:                                                                          |
    * |                  http://www.phpguru.org/static/license.html                  |
    * o------------------------------------------------------------------------------o
    *
    * © Copyright 2008 Richard Heyes
    */

    /**
    * Timer class for debugging
    */
    
class Timer
    
{
        
/**
        * Array of timing information
        * @var array
        */
        
var $times;
    
        
/**
        * Constructor Sets the start time
        */
        
function __construct()
        {
            
$time explode(' 'microtime());
            
$this->times['Start'] = $time[0] + $time[1];
        }
        
        function 
Timer()
        {
            
$this->__construct();
        }
        
        
/**
        * Sets an interval time
        * 
        * @param string $label Label for this checkpoint
        */
        
function Interval($label)
        {
            
$time explode(' 'microtime());
            
$this->times[$label] = $time[0] + $time[1];
        }
        
        
/**
        * Alias for the above
        * 
        * @param string $label Label for this checkpoint
        */
        
function CheckPoint($label)
        {
            
$this->interval($label);
        }
        
        
/**
        * Dumps the times in HTML format (by default)
        * 
        * @param string $format Format that output is desired in
        */
        
function Dump($format 'text')
        {
            
$time explode(' 'microtime());
            
$this->times['End'] = $time[0] + $time[1];
    
            
/**
            * Build the output
            */
            
require_once('Console/Table.php');
            
$table = new Console_Table();
            
$table->setHeaders(array('Checkpoint''Time''Difference'));
            
            
$lastCheckpoint 'Start';
            
$lastTime       0;
            foreach (
$this->times as $k => $v) {
                
$time str_pad(sprintf('%0.04f'$v $this->times[$lastCheckpoint]), 6'0'STR_PAD_RIGHT);
                
$table->addRow(array($k$timestr_pad($time $lastTime6'0'STR_PAD_RIGHT)));
                
$lastTime $time;
            }
            
            echo 
'<pre>' $table->getTable() . '</pre>';
        }
        
        
/**
        * Aliases for the above
        */
        
function End()    { $this->Dump();}
        function 
Finish() { $this->Dump();}
        function 
Stop()   { $this->Dump();}
    }
?>