Re: Benchmark_Timer patch
| From: | Ludovico Magnocavallo | Date: | Wed, 23 Jan 2002 16:15:26 +0000 |
| Subject: | Re: Benchmark_Timer patch | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-4119@lists.php.net to get a copy of this message | ||
diff -u patch
--- Timer_orig.php Wed Jan 23 16:05:19 2002 +++ Timer.php Wed Jan 23 17:14:01 2002 @@ -28,33 +28,71 @@ * * Example: * - * $timer = new Benchmark_Timer; - * - * $timer->start(); + * $timer =& new Benchmark_Timer; * $timer->setMarker('Marker 1'); - * $timer->stop(); * - * $profiling = $timer->getProfiling(); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @version $Revision: 1.1 $ * @access public */ -class Benchmark_Timer { +require_once('PEAR.php'); + +class Benchmark_Timer extends PEAR { /** * Contains the markers * * @var array - * @access public + * @access private */ var $markers = array(); /** + * Constructor, starts profiling recording + * + * @access public + */ + function Benchmark_Timer() { + $this->PEAR(); + $this->start(); + } + + /** + * Destructor, stops profiling recording + * + * @access private + */ + function _Benchmark_Timer() { + $this->stop(); + echo($this->pretty_format()); + } + + /** + * Return formatted profiling information. + * + * @see getProfiling() + * @access public + */ + function pretty_format() { + $total = $this->TimeElapsed(); + $result = $this->getProfiling(); + $out = "<table border=1>\n"; + $out .= "<tr><td> </td><td><b>time index</b></td><td><b>ex time</b></td><td><b>%</b></td></tr>\n"; + foreach ($result as $k => $v) { + $perc = (($v['diff'] * 100) / $total); + $out .= "<tr><td><b>" . $v['name'] . "</b></td><td>" . $v['time'] . "</td><td>" . $v['diff'] . "</td><td>" . number_format($perc, 2, '.', '') . "%</td></tr>\n"; + } + $out .= "<tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td></tr>\n"; + $out .= "</table>\n"; + return $out; + } + + /** * Set "Start" marker. * * @see setMarker(), stop() - * @access public + * @access private */ function start() { $this->setMarker('Start'); @@ -64,7 +102,7 @@ * Set "Stop" marker. * * @see setMarker(), start() - * @access public + * @access private */ function stop() { $this->setMarker('Stop'); @@ -115,16 +153,12 @@ $result = array(); foreach ($this->markers as $marker => $time) { - if ($marker == 'Start') { - $diff = '-'; + if (extension_loaded('bcmath')) { + $diff = bcsub($time, $temp, 6); + $total = bcadd($total, $diff, 6); } else { - if (extension_loaded('bcmath')) { - $diff = bcsub($time, $temp, 6); - $total = bcadd($total, $diff, 6); - } else { - $diff = $time - $temp; - $total = $total + $diff; - } + $diff = $time - $temp; + $total = $total + $diff; } $result[$i]['name'] = $marker; @@ -135,7 +169,7 @@ $temp = $time; $i++; } - + $result[0]['diff'] = '-'; return $result; } }
--- Timer_orig.php Wed Jan 23 16:05:19 2002 +++ Timer.php Wed Jan 23 17:14:01 2002 @@ -28,33 +28,71 @@ * * Example: * - * $timer = new Benchmark_Timer; - * - * $timer->start(); + * $timer =& new Benchmark_Timer; * $timer->setMarker('Marker 1'); - * $timer->stop(); * - * $profiling = $timer->getProfiling(); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @version $Revision: 1.1 $ * @access public */ -class Benchmark_Timer { +require_once('PEAR.php'); + +class Benchmark_Timer extends PEAR { /** * Contains the markers * * @var array - * @access public + * @access private */ var $markers = array(); /** + * Constructor, starts profiling recording + * + * @access public + */ + function Benchmark_Timer() { + $this->PEAR(); + $this->start(); + } + + /** + * Destructor, stops profiling recording + * + * @access private + */ + function _Benchmark_Timer() { + $this->stop(); + echo($this->pretty_format()); + } + + /** + * Return formatted profiling information. + * + * @see getProfiling() + * @access public + */ + function pretty_format() { + $total = $this->TimeElapsed(); + $result = $this->getProfiling(); + $out = "<table border=1>\n"; + $out .= "<tr><td> </td><td><b>time index</b></td><td><b>ex time</b></td><td><b>%</b></td></tr>\n"; + foreach ($result as $k => $v) { + $perc = (($v['diff'] * 100) / $total); + $out .= "<tr><td><b>" . $v['name'] . "</b></td><td>" . $v['time'] . "</td><td>" . $v['diff'] . "</td><td>" . number_format($perc, 2, '.', '') . "%</td></tr>\n"; + } + $out .= "<tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td></tr>\n"; + $out .= "</table>\n"; + return $out; + } + + /** * Set "Start" marker. * * @see setMarker(), stop() - * @access public + * @access private */ function start() { $this->setMarker('Start'); @@ -64,7 +102,7 @@ * Set "Stop" marker. * * @see setMarker(), start() - * @access public + * @access private */ function stop() { $this->setMarker('Stop'); @@ -115,16 +153,12 @@ $result = array(); foreach ($this->markers as $marker => $time) { - if ($marker == 'Start') { - $diff = '-'; + if (extension_loaded('bcmath')) { + $diff = bcsub($time, $temp, 6); + $total = bcadd($total, $diff, 6); } else { - if (extension_loaded('bcmath')) { - $diff = bcsub($time, $temp, 6); - $total = bcadd($total, $diff, 6); - } else { - $diff = $time - $temp; - $total = $total + $diff; - } + $diff = $time - $temp; + $total = $total + $diff; } $result[$i]['name'] = $marker; @@ -135,7 +169,7 @@ $temp = $time; $i++; } - + $result[0]['diff'] = '-'; return $result; } }