Zend - The PHP Company




Date & Time

Add Code


Stopwatch class for checking PHP performance  

Type: class
Added by: hughprior
Entered: 16/04/2002
Last modified: 04/12/2001
Rating: - (fewer than 3 votes)
Views: 6393
This simple class gives stopwatch style functionality, including split times, to enable the timing of functions. It's easy to understand and use (I think!), but you could easily expand it.


<? 

/**
* Simple stopwatch, useful for timing PHP code execution, includes split times
*
* Example use:
*    $stopwatch = new stopwatch;
*    ...
*    // Pass whatever comment you like 
*    $stopwatch->lap("in my_func() at start");
*     ...
*    $stopwatch->lap("in my_func() in middle");
*    ...
*    $stopwatch->lap("in my_func() at end");
*
* Example output:
*     in my_func(), at start split time 0.00 seconds
*     in my_func(), at start elapsed time 0.00 seconds
*
*     in my_func(), in middle complete split time 6.49 seconds
*     in my_func(), in middle complete elapsed time 6.49 seconds
*
*   in my_func() at end split time 2.58 seconds
*   in my_func() at end elapsed time 9.07 seconds
*
* @author Hugh Prior    stopwatch@priorwebsites.com
*/
class stopwatch

    
//properties 
    
var $start_time
    var 
$stop_time
    
    var 
$lap_times;
    
    
/**
    * Initialise stopwatch by starting it going
    */
    
function stopwatch()
    {
        
$this->start();
        
$this->lap_times = array();
    }

    
/**
    /* get_microtime function taken from Everett Michaud on Zend.com 
    */
    
function get_microtime()
    { 
        list(
$secs$micros) = split(" "microtime()); 
        
$mt $secs $micros
        
        return 
$mt
    }
  
      
/**
    * Set start time
    */
    
function start()
    { 
        
$this->start_time $this->get_microtime(); 
    }
  
      
/**
    * Set end time
    */
    
function stop()
    { 
        
$this->stop_time $this->get_microtime(); 
    }
  
      
/**
    * Get the elapsed time
    */
    
function get_elapsed()
    {
        
$time_now $this->get_microtime();
        
$elapsed $time_now  -  $this->start_time;
        
        return 
$elapsed;
    }

    
/**
    * Get the last split (lap) time
    */
    
function get_last_split()
    {
        
$time_now $this->get_microtime();
        
$laps_done sizeof($this->lap_times);
        if (
$laps_done == 0) {
            
$split $time_now $this->start_time;
        }
        else {
            
$split_no sizeof($this->lap_times) - 1;
            
$split $time_now $this->lap_times[$split_no];
        }
        
$this->lap_times[] = $time_now;
        
        return 
$split;
    }
        
    
/**
    * Look at the times (i.e. print it!)
    */
    
function lap($comment="")
    {
        
$split $this->get_last_split();
        
$split number_format($split2);
        
        
$elapsed $this->get_elapsed();
        
$elapsed number_format($elapsed2);
        
        print 
"$comment split time $split seconds<br>";
        print 
"$comment elapsed time $elapsed seconds<br>";
        print 
"<br>";
        
    }
}
?> 


Usage Example




Rate This Script





Search



This Category All Categories