summaryrefslogtreecommitdiff
path: root/pear/Timer.php
blob: f5088ae26bd38a2584fa40136c3970eed95eb7c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Sebastian Bergmann <sb@phpOpenTracker.de>                   |
// +----------------------------------------------------------------------+
//
// $Id$
//

  /**
  * PEAR/Timer
  * 
  * Purpose:
  * 
  *   Timing Script Execution, Generating Profiling Information
  * 
  * Example:
  * 
  *   $timer = new Timer;
  * 
  *   $timer->start();
  *   $timer->set_marker( "Marker 1" );
  *   $timer->stop();
  * 
  *   $profiling = $timer->get_profiling();
  * 
  * @author   Sebastian Bergmann <sb@phpOpenTracker.de>
  * @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.
    *
    * @param    string  name of the marker to be set
    * @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 );
    }
    
    // }}}
    // {{{ 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()
    {
      $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 );
        }

        $result[ $i ][ "name"  ] = $marker;
        $result[ $i ][ "time"  ] = $time;
        $result[ $i ][ "diff"  ] = $diff;
        $result[ $i ][ "total" ] = $total;

        $temp = $time;
        $i++;
      }

      return $result;
    }

    // }}}
  }
?>