cvs: php4 /pear/Benchmark Timer.php

From: Date: Wed, 03 Jan 2001 05:10:20 +0000
Subject: cvs: php4 /pear/Benchmark Timer.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-3335@lists.php.net to get a copy of this message
chagenbu Tue Jan 2 21:10:20 2001 EDT Modified files: /php4/pear/Benchmark Timer.php Log: match coding standards, and be nice enough to work when bcmath isn't loaded. Index: php4/pear/Benchmark/Timer.php diff -u php4/pear/Benchmark/Timer.php:1.3 php4/pear/Benchmark/Timer.php:1.4 --- php4/pear/Benchmark/Timer.php:1.3 Fri Sep 29 01:29:22 2000 +++ php4/pear/Benchmark/Timer.php Tue Jan 2 21:10:20 2001 @@ -16,154 +16,143 @@ // | Authors: Sebastian Bergmann <sb@phpOpenTracker.de> | // +----------------------------------------------------------------------+ // -// $Id: Timer.php,v 1.3 2000/09/29 08:29:22 sbergmann Exp $ +// $Id: Timer.php,v 1.4 2001/01/03 05:10:20 chagenbu Exp $ // - /** - * Benchmark::Timer - * - * Purpose: - * - * Timing Script Execution, Generating Profiling Information - * - * Example: - * - * $timer = new Benchmark_Timer; - * - * $timer->start(); - * $timer->set_marker( "Marker 1" ); - * $timer->stop(); - * - * $profiling = $timer->get_profiling(); - * - * @author Sebastian Bergmann <sb@phpOpenTracker.de> - * @version $Revision: 1.3 $ - * @access public - */ - - class Benchmark_Timer - { - // {{{ properties - +/** + * Benchmark::Timer + * + * Purpose: + * + * Timing Script Execution, Generating Profiling Information + * + * Example: + * + * $timer = new Benchmark_Timer; + * + * $timer->start(); + * $timer->setMarker('Marker 1'); + * $timer->stop(); + * + * $profiling = $timer->getProfiling(); + * + * @author Sebastian Bergmann <sb@phpOpenTracker.de> + * @version $Revision: 1.4 $ + * @access public + */ +class Benchmark_Timer +{ + /** - * Contains the markers - * - * @var array - * @access public - */ - + * Contains the markers + * + * @var array + * @access public + */ var $markers = array(); - // }}} - // {{{ start() - + /** - * Set "Start" marker. - * - * @see set_marker(), stop() - * @access public - */ - + * Constructor + */ + function Benchmark_Timer() + { + } + + + /** + * Set "Start" marker. + * + * @see setMarker(), stop() + * @access public + */ function start() { - $this->set_marker( "Start" ); + $this->setMarker('Start'); } - - // }}} - // {{{ stop() - + /** - * Set "Stop" marker. - * - * @see set_marker(), start() - * @access public - */ - + * Set "Stop" marker. + * + * @see setMarker(), start() + * @access public + */ function stop() { - $this->set_marker( "Stop" ); + $this->setMarker('Stop'); } - - // }}} - // {{{ set_marker() - + /** - * Set marker. - * - * @param string name of the marker to be set - * @see start(), stop() - * @access public - */ - - function set_marker( $name ) + * Set marker. + * + * @param string name of the marker to be set + * @see start(), stop() + * @access public + */ + function setMarker($name) { - $microtime = explode( " ", microtime() ); - $this->markers[ $name ] = $microtime[ 1 ] . substr( $microtime[ 0 ], 1 ); + $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" ) + * 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 timeElapsed($start = 'Start', $end = 'Stop') { - return bcsub( $this->markers[ $end ], $this->markers[ $start ], 6 ); + if (extension_loaded('bcmath')) { + return bcsub($this->markers[$end], $this->markers[$start], 6); + } else { + return $this->markers[$end] - $this->markers[$start]; + } } - // }}} - // {{{ get_profiling() - /** - * Returns profiling information. - * - * $profiling[ x ][ "name" ] = name of marker x - * $profiling[ x ][ "time" ] = time index of marker x - * $profiling[ x ][ "diff" ] = execution time from marker x-1 to this marker x - * $profiling[ x ][ "total" ] = total execution time up to marker x - * - * @return array $profiling - * @access public - */ - - function get_profiling() + * Returns profiling information. + * + * $profiling[x]['name'] = name of marker x + * $profiling[x]['time'] = time index of marker x + * $profiling[x]['diff'] = execution time from marker x-1 to this marker x + * $profiling[x]['total'] = total execution time up to marker x + * + * @return array $profiling + * @access public + */ + function getProfiling() { - $i = 0; - $total = 0; - $result = array(); - - while( list( $marker, $time ) = each( $this->markers ) ) - { - if( $marker == "Start" ) - { - $diff = "-"; - } - - else - { - $diff = bcsub( $time, $temp, 6 ); - $total = bcadd( $total, $diff, 6 ); + $i = 0; + $total = 0; + $result = array(); + + foreach ($this->markers as $marker => $time) { + if ($marker == 'Start') { + $diff = '-'; + } else { + if (extension_loaded('bcmath')) { + $diff = bcsub($time, $temp, 6); + $total = bcadd($total, $diff, 6); + } else { + $diff = $time - $temp; + $total = $total + $diff; + } + } + + $result[$i]['name'] = $marker; + $result[$i]['time'] = $time; + $result[$i]['diff'] = $diff; + $result[$i]['total'] = $total; + + $temp = $time; + $i++; } - - $result[ $i ][ "name" ] = $marker; - $result[ $i ][ "time" ] = $time; - $result[ $i ][ "diff" ] = $diff; - $result[ $i ][ "total" ] = $total; - - $temp = $time; - $i++; - } - - return $result; + + return $result; } - - // }}} - } + +} ?>

« previous php.cvs (#3335) next »