Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * sfTimer class allows to time some PHP code.
13
 *
14
 * @package    symfony
15
 * @subpackage util
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfTimer.class.php 9079 2008-05-20 00:38:07Z Carl.Vondrick $
18
 */
19
class sfTimer
20
{
21
  protected
22
    $startTime = null,
23
    $totalTime = null,
24
    $name = '',
25
    $calls = 0;
26
 
27
  /**
28
   * Creates a new sfTimer instance.
29
   *
30
   * @param string $name The name of the timer
31
   */
32
  public function __construct($name = '')
33
  {
34
    $this->name = $name;
35
    $this->startTimer();
36
  }
37
 
38
  /**
39
   * Starts the timer.
40
   */
41
  public function startTimer()
42
  {
43
    $this->startTime = microtime(true);
44
  }
45
 
46
  /**
47
   * Stops the timer and add the amount of time since the start to the total time.
48
   *
49
   * @return float Time spend for the last call
50
   */
51
  public function addTime()
52
  {
53
    $spend = microtime(true) - $this->startTime;
54
    $this->totalTime += $spend;
55
    ++$this->calls;
56
 
57
    return $spend;
58
  }
59
 
60
  /**
61
   * Gets the number of calls this timer has been called to time code.
62
   *
63
   * @return integer Number of calls
64
   */
65
  public function getCalls()
66
  {
67
    return $this->calls;
68
  }
69
 
70
  /**
71
   * Gets the total time elapsed for all calls of this timer.
72
   *
73
   * @return float Time in seconds
74
   */
75
  public function getElapsedTime()
76
  {
77
    if (null === $this->totalTime)
78
    {
79
      $this->addTime();
80
    }
81
 
82
    return $this->totalTime;
83
  }
84
}