[ Index ]

krapohl.info

title

Body

[close]

/pnincludes/Benchmark/ -> Timer.php (source)

   1  <?php
   2  //
   3  // +----------------------------------------------------------------------+
   4  // | PHP version 4.0                                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997-2001 The PHP Group                                |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.02 of the PHP license,      |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available at through the world-wide-web at                           |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  17  // +----------------------------------------------------------------------+
  18  //
  19  // $Id: Timer.php,v 1.1 2004/02/11 17:42:38 larsneo Exp $
  20  //
  21  
  22  /**
  23   * Benchmark::Timer
  24   * 
  25   * Purpose:
  26   * 
  27   *     Timing Script Execution, Generating Profiling Information
  28   * 
  29   * Example:
  30   * 
  31   *     $timer = new Benchmark_Timer;
  32   * 
  33   *     $timer->start();
  34   *     $timer->setMarker('Marker 1');
  35   *     $timer->stop();
  36   * 
  37   *     $profiling = $timer->getProfiling();
  38   * 
  39   * @author   Sebastian Bergmann <sb@sebastian-bergmann.de>
  40   * @version  $Revision: 1.1 $
  41   * @access   public
  42   */
  43  
  44  class Benchmark_Timer {
  45      /**
  46       * Contains the markers
  47       *
  48       * @var    array
  49       * @access public
  50       */
  51      var $markers = array();
  52  
  53      /**
  54       * Set "Start" marker.
  55       *
  56       * @see    setMarker(), stop()
  57       * @access public
  58       */
  59      function start() {
  60          $this->setMarker('Start');
  61      }
  62  
  63      /**
  64       * Set "Stop" marker.
  65       *
  66       * @see    setMarker(), start()
  67       * @access public
  68       */
  69      function stop() {
  70          $this->setMarker('Stop');
  71      }
  72  
  73      /**
  74       * Set marker.
  75       *
  76       * @param  string  name of the marker to be set
  77       * @see    start(), stop()
  78       * @access public
  79       */
  80      function setMarker($name) {
  81          $microtime = explode(' ', microtime());
  82          $this->markers[$name] = $microtime[1] . substr($microtime[0], 1);
  83      }
  84  
  85      /**
  86       * Returns the time elapsed betweens two markers.
  87       *
  88       * @param  string  $start        start marker, defaults to "Start"
  89       * @param  string  $end          end marker, defaults to "Stop"
  90       * @return double  $time_elapsed time elapsed between $start and $end
  91       * @access public
  92       */
  93      function timeElapsed($start = 'Start', $end = 'Stop') {
  94          if (extension_loaded('bcmath')) {
  95              return bcsub($this->markers[$end], $this->markers[$start], 6);
  96          } else {
  97              return $this->markers[$end] - $this->markers[$start];
  98          }
  99      }
 100  
 101      /**
 102       * Returns profiling information.
 103       *
 104       * $profiling[x]['name']  = name of marker x
 105       * $profiling[x]['time']  = time index of marker x
 106       * $profiling[x]['diff']  = execution time from marker x-1 to this marker x
 107       * $profiling[x]['total'] = total execution time up to marker x
 108       *
 109       * @return array $profiling
 110       * @access public
 111       */
 112      function getProfiling() {
 113          $i = 0;
 114          $total = 0;
 115          $result = array();
 116          
 117          foreach ($this->markers as $marker => $time) {
 118              if ($marker == 'Start') {
 119                  $diff = '-';
 120              } else {
 121                  if (extension_loaded('bcmath')) {
 122                      $diff  = bcsub($time,  $temp, 6);
 123                      $total = bcadd($total, $diff, 6);
 124                  } else {
 125                      $diff  = $time - $temp;
 126                      $total = $total + $diff;
 127                  }
 128              }
 129              
 130              $result[$i]['name']  = $marker;
 131              $result[$i]['time']  = $time;
 132              $result[$i]['diff']  = $diff;
 133              $result[$i]['total'] = $total;
 134              
 135              $temp = $time;
 136              $i++;
 137          }
 138  
 139          return $result;
 140      }
 141  }
 142  ?>


Generated: Wed Feb 16 22:40:07 2005 Cross-referenced by PHPXref 0.6