1. sfTimer.class.php
  2. /** * sfTimer class allows to time some PHP code. * * @package symfony * @subpackage util * @author Fabien Potencier * @version SVN: $Id: sfTimer.class.php 9079 2008-05-20 00:38:07Z Carl.Vondrick $ */
  3. class sfTimer
  4. {
  5. protected
  6. $startTime = null,
  7. $totalTime = null,
  8. $name = '',
  9. $calls = 0;
  10. /**
  11. * Creates a new sfTimer instance.
  12. *
  13. * @param string $name The name of the timer
  14. */
  15. public function __construct($name = '')
  16. {
  17. $this->name = $name;
  18. $this->startTimer();
  19. }
  20. /**
  21. * Starts the timer.
  22. */
  23. public function startTimer()
  24. {
  25. $this->startTime = microtime(true);
  26. }
  27. /**
  28. * Stops the timer and add the amount of time since the start to the total time.
  29. *
  30. * @return float Time spend for the last call
  31. */
  32. public function addTime()
  33. {
  34. $spend = microtime(true) - $this->startTime;
  35. $this->totalTime += $spend;
  36. ++$this->calls;
  37. return $spend;
  38. }
  39. /**
  40. * Gets the number of calls this timer has been called to time code.
  41. *
  42. * @return integer Number of calls
  43. */
  44. public function getCalls()
  45. {
  46. return $this->calls;
  47. }
  48. /**
  49. * Gets the total time elapsed for all calls of this timer.
  50. *
  51. * @return float Time in seconds
  52. */
  53. public function getElapsedTime()
  54. {
  55. if (null === $this->totalTime)
  56. {
  57. $this->addTime();
  58. }
  59. return $this->totalTime;
  60. }
  61. }

Debug toolbar