From ee11aabc30ff54ef86a8af0e9a18aebf1024440f Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Thu, 28 Sep 2000 07:21:21 +0000 Subject: [PATCH] Added PEAR/Timer class for timing script execution and generating profiling information. This class is based upon a concept by Allan Kent shown in his article on PHPBuilder.com --- pear/Timer.php | 153 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 pear/Timer.php diff --git a/pear/Timer.php b/pear/Timer.php new file mode 100644 index 0000000000..456e31c723 --- /dev/null +++ b/pear/Timer.php @@ -0,0 +1,153 @@ + | +// | (based upon code by Allan Kent ) | +// +----------------------------------------------------------------------+ +// +// $Id$ +// + + /** + * PEAR/Timer + * + * Purpose: + * + * Timing Script Execution, Generating Profiling Information + * + * Example: + * + * $timer = new Timer; + * + * $timer->start(); + * $timer->set_marker( "Marker 1" ); + * $timer->stop(); + * + * $timer->profiling_information(); + * + * @author Sebastian Bergmann + * @version 1.0 28/09/00 + * @access public + */ + + class Timer + { + // {{{ properties + + /** + * Contains the markers + * @var array + * @access public + */ + + var $markers = array(); + + // }}} + // {{{ start() + + /** + * Set "Start" marker. + * @brother stop() + * @access public + */ + + function start() + { + $this->set_marker( "Start" ); + } + + // }}} + // {{{ stop() + + /** + * Set "Stop" marker. + * @brother start() + * @access public + */ + + function stop() + { + $this->set_marker( "Stop" ); + } + + // }}} + // {{{ set_marker() + + /** + * Set marker. + * @brother stop() + * @access public + */ + + function set_marker( $name ) + { + $microtime = explode( " ", microtime() ); + $this->markers[ $name ] = $microtime[ 1 ] . substr( $microtime[ 0 ], 1 ); + } + + // }}} + // {{{ time_elapsed() + + /** + * Returns the time elapsed betweens two markers. + * @param string $start start marker, defaults to "Start" + * @param string $end end marker, defaults to "Stop" + * @return double $time_elapsed time elapsed between $start and $end + * @access public + */ + + function time_elapsed( $start = "Start", $end = "Stop" ) + { + return bcsub( $this->markers[ $end ], $this->markers[ $start ], 6 ); + } + + // }}} + // {{{ profiling_information() + + /** + * Prints profiling information. + * @access public + */ + + function profiling_information() + { + print ""; + + while( list( $marker, $time ) = each( $this->markers ) ) + { + print ""; + print ""; + + if( $marker == "Start" ) + { + $diff = "-"; + } + + else + { + $diff = bcsub( $time, $temp, 6 ); + } + + print ""; + + $temp = $time; + } + + print "
MarkerTimeDiff
" . $marker . "" . $time . "" . $diff . "
 Total:" . $this->time_elapsed() . "
"; + } + + // }}} + } +?> -- 2.50.1