Source for file timer-defs.php

Documentation is available at timer-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: timer-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for a simple micro-timer */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package timer *//**
  27. * The microtimer class
  28. * A generic microtimer. This timer allows elapsed times to be
  29. * measured down to microseconds in theory, although depending
  30. * on how 'real-time' the OS is, there may well be limitations.
  31. * The microtimer works like a stopwatch. It is either ticking
  32. * or it is stopped. It has a number of seconds on the clock
  33. * at any one instant. It may be started, stopped or read.
  34. * @package datetime
  35. */
  36. class microtimer {
  37. // Public
  38. /** Current number of seconds on the timer */
  39.  
  40. var $seconds = 0;
  41. /** True if the timer is ticking */
  42.  
  43. var $ticking = false;
  44.  
  45. // Private
  46. /** Reference time to base elapsed interval on
  47. @access private */
  48. var $reference = 0;
  49. // .....................................................................
  50. /**
  51. * Constructor
  52. * Create a new microtimer.
  53. */
  54. function microtimer() {
  55. $this->reset();
  56. } // microtimer
  57. // .....................................................................
  58. /**
  59. * Return the current microtime
  60. * @return integer Seconds on the system clock right now
  61. * @access private
  62. */
  63. function get_microtime() {
  64. list($usec, $sec) = explode(" ", microtime());
  65. return (float)$sec + (float)$usec;
  66. } // get_microtime
  67. // .....................................................................
  68. /**
  69. * Update the timer values
  70. * Updates the internal seconds on the clock.
  71. * @access private
  72. */
  73. function update() {
  74. $seconds = $this->get_microtime();
  75. if ($this->ticking) {
  76. $delta = $seconds - $this->reference;
  77. $this->seconds += $delta;
  78. }
  79. $this->reference = $seconds;
  80. } // update
  81. // .....................................................................
  82. /**
  83. * Start the timer. Starts the timer ticking. If it was already
  84. * ticking then there is no change in status.
  85. */
  86. function start() {
  87. $this->update();
  88. $this->ticking = true;
  89. } // start
  90. // .....................................................................
  91. /**
  92. * Stop the timer
  93. * This freezes the internal seconds value, and stops the timer
  94. * from ticking.
  95. */
  96. function stop() {
  97. $this->update();
  98. $this->ticking = false;
  99. } // stop
  100. // .....................................................................
  101. /**
  102. * Reset the timer. This zeroes the timer. If it was ticking it goes
  103. * on ticking, but starting from zero.
  104. */
  105. function reset() {
  106. $this->update();
  107. $this->seconds = 0;
  108. } // reset
  109. // .....................................................................
  110. /**
  111. * Restart the timer. This zeroes the timer and then starts it. Has
  112. * same effect as reset() but also makes sure the timer is ticking
  113. * after resetting internal timer values.
  114. */
  115. function restart() {
  116. $this->reset();
  117. $this->start();
  118. } // restart
  119. // .....................................................................
  120. /**
  121. * Returns the seconds on the clock as a floating point number.
  122. * @return float Seconds on the clock at the time of this call.
  123. */
  124. function secs() {
  125. $this->update();
  126. return $this->seconds;
  127. } // secs
  128. // .....................................................................
  129. /**
  130. * Returns the milli-seconds on the clock.
  131. * @return float Milli-seconds on the clock at the time of this call.
  132. */
  133. function millisecs() {
  134. $this->update();
  135. return $this->seconds * 1000;
  136. } // millisecs
  137. // .....................................................................
  138. /**
  139. * Returns the micro-seconds on the clock.
  140. * @return float Micro-seconds on the clock at the time of this call.
  141. */
  142. function microsecs() {
  143. $this->update();
  144. return $this->seconds * 1000000;
  145. } // microsecs
  146. // .....................................................................
  147. /**
  148. * Return the time on the clock in a nice 'human' format. This is in
  149. * the form of a string, viz: '14d 2h 12m 13s'.
  150. * @return string The time on the clock in 'human' format.
  151. */
  152. function formatted_time() {
  153. $this->update();
  154. return nicetime($this->secs());
  155. } // formatted_time
  156. // .....................................................................
  157. /**
  158. * Return the seconds on the clock to 2 decimal places.
  159. * Note that this is a numeric string which includes commas for the
  160. * thousands. It is therefore not a float suitable for arithmetic.
  161. * @return string Formatted number: seconds on the clock to 2 decimal places.
  162. */
  163. function formatted_secs() {
  164. $this->update();
  165. return number_format($this->secs(), 2);
  166. } // formatted_secs
  167. // .....................................................................
  168. /**
  169. * Return the milli-seconds on the clock to 2 decimal places.
  170. * Note that this is a numeric string which includes commas for the
  171. * thousands. It is therefore not a float suitable for arithmetic.
  172. * @return string Formatted number: milli-seconds on the clock to 2 decimal places.
  173. */
  174. function formatted_millisecs() {
  175. $this->update();
  176. return number_format($this->millisecs(), 2);
  177. } // formatted_millisecs
  178. // .....................................................................
  179. /**
  180. * Return the micro-seconds on the clock to 2 decimal places.
  181. * Note that this is a numeric string which includes commas for the
  182. * thousands. It is therefore not a float suitable for arithmetic.
  183. * @return string Formatted number: micro-seconds on the clock to 2 decimal places.
  184. */
  185. function formatted_microsecs() {
  186. $this->update();
  187. return number_format($this->microsecs(), 2);
  188. } // formatted_microsecs
  189.  
  190. } // timer class
  191. //-----------------------------------------------------------------------
  192.  
  193. ?>

Documentation generated by phpDocumentor 1.3.0RC3