Development

/branches/1.0/lib/debug/sfTimer.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/debug/sfTimer.class.php

Revision 3411, 1.6 kB (checked in by fabien, 7 years ago)

added return value to sfTimer::addTime() (closes #1400 - patch from gucky)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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 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 integer 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 integer Time in milliseconds
74    */
75   public function getElapsedTime()
76   {
77     if (null === $this->totalTime)
78     {
79       $this->addTime();
80     }
81
82     return $this->totalTime;
83   }
84 }
85
Note: See TracBrowser for help on using the browser.