1: <?php
2: /**
3: * Simple interface for timing operations.
4: *
5: * <code>
6: * $t = new Horde_Support_Timer;
7: * $t->push();
8: * $elapsed = $t->pop();
9: * </code>
10: *
11: * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
12: *
13: * @category Horde
14: * @package Support
15: * @license http://www.horde.org/licenses/bsd
16: */
17: class Horde_Support_Timer
18: {
19: /**
20: * Holds the starting timestamp.
21: *
22: * @var array
23: */
24: protected $_start = array();
25:
26: /**
27: * Current index for stacked timers.
28: *
29: * @var integer
30: */
31: protected $_idx = 0;
32:
33: /**
34: * Push a new timer start on the stack.
35: */
36: public function push()
37: {
38: $start = $this->_start[$this->_idx++] = microtime(true);
39: return $start;
40: }
41:
42: /**
43: * Pop the latest timer start and return the difference with the current
44: * time.
45: *
46: * @return float The amount of time passed.
47: */
48: public function pop()
49: {
50: $etime = microtime(true);
51:
52: if (! ($this->_idx > 0)) {
53: throw new Exception('No timers have been started');
54: }
55:
56: return $etime - $this->_start[--$this->_idx];
57: }
58:
59: }
60: