Timer.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * $Id: Timer.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. /**
  22. * This class can be used to obtain the execution time of all of the scripts
  23. * that are executed in the process of building a page.
  24. *
  25. * Example:
  26. * To be done before any scripts execute:
  27. *
  28. * $Timer = new Timer;
  29. * $Timer->Start_Timer();
  30. *
  31. * To be done after all scripts have executed:
  32. *
  33. * $timer->Stop_Timer();
  34. * $timer->Get_Elapsed_Time(int number_of_places);
  35. *
  36. * @author Charles Killian
  37. * @author Hans Lellelid <hans@xmpl.org>
  38. * @package phing.system.util
  39. * @version $Revision: 905 $ $Date: 2010-10-05 18:28:03 +0200 (Tue, 05 Oct 2010) $
  40. */
  41. class Timer {
  42. /** start time */
  43. protected $stime;
  44. /** end time */
  45. protected $etime;
  46. /**
  47. * This function sets the class variable $stime to the current time in
  48. * microseconds.
  49. * @return void
  50. */
  51. public function start() {
  52. $this->stime = $this->getMicrotime();
  53. }
  54. /**
  55. * This function sets the class variable $etime to the current time in
  56. * microseconds.
  57. * @return void
  58. */
  59. function stop() {
  60. $this->etime = $this->getMicrotime();
  61. }
  62. /**
  63. * This function returns the elapsed time in seconds.
  64. *
  65. * Call start_time() at the beginning of script execution and end_time() at
  66. * the end of script execution. Then, call elapsed_time() to obtain the
  67. * difference between start_time() and end_time().
  68. *
  69. * @param $places decimal place precision of elapsed time (default is 5)
  70. * @return string Properly formatted time.
  71. */
  72. function getElapsedTime($places=5) {
  73. $etime = $this->etime - $this->stime;
  74. $format = "%0.".$places."f";
  75. return (sprintf ($format, $etime));
  76. }
  77. /**
  78. * This function returns the current time in microseconds.
  79. *
  80. * @author Everett Michaud, Zend.com
  81. * @return current time in microseconds
  82. * @access private
  83. */
  84. function getMicrotime() {
  85. list($usec, $sec) = explode(" ", microtime());
  86. return ((float)$usec + (float)$sec);
  87. }
  88. }