Timer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __TIMER_H__
  21. #define __TIMER_H__
  22. /*
  23. ===============================================================================
  24. Clock tick counter. Should only be used for profiling.
  25. ===============================================================================
  26. */
  27. class idTimer {
  28. public:
  29. idTimer( void );
  30. idTimer( double clockTicks );
  31. ~idTimer( void );
  32. idTimer operator+( const idTimer &t ) const;
  33. idTimer operator-( const idTimer &t ) const;
  34. idTimer & operator+=( const idTimer &t );
  35. idTimer & operator-=( const idTimer &t );
  36. void Start( void );
  37. void Stop( void );
  38. void Clear( void );
  39. double ClockTicks( void ) const;
  40. double Milliseconds( void ) const;
  41. private:
  42. static double base;
  43. enum {
  44. TS_STARTED,
  45. TS_STOPPED
  46. } state;
  47. double start;
  48. double clockTicks;
  49. void InitBaseClockTicks( void ) const;
  50. };
  51. /*
  52. =================
  53. idTimer::idTimer
  54. =================
  55. */
  56. ID_INLINE idTimer::idTimer( void ) {
  57. state = TS_STOPPED;
  58. clockTicks = 0.0;
  59. }
  60. /*
  61. =================
  62. idTimer::idTimer
  63. =================
  64. */
  65. ID_INLINE idTimer::idTimer( double _clockTicks ) {
  66. state = TS_STOPPED;
  67. clockTicks = _clockTicks;
  68. }
  69. /*
  70. =================
  71. idTimer::~idTimer
  72. =================
  73. */
  74. ID_INLINE idTimer::~idTimer( void ) {
  75. }
  76. /*
  77. =================
  78. idTimer::operator+
  79. =================
  80. */
  81. ID_INLINE idTimer idTimer::operator+( const idTimer &t ) const {
  82. assert( state == TS_STOPPED && t.state == TS_STOPPED );
  83. return idTimer( clockTicks + t.clockTicks );
  84. }
  85. /*
  86. =================
  87. idTimer::operator-
  88. =================
  89. */
  90. ID_INLINE idTimer idTimer::operator-( const idTimer &t ) const {
  91. assert( state == TS_STOPPED && t.state == TS_STOPPED );
  92. return idTimer( clockTicks - t.clockTicks );
  93. }
  94. /*
  95. =================
  96. idTimer::operator+=
  97. =================
  98. */
  99. ID_INLINE idTimer &idTimer::operator+=( const idTimer &t ) {
  100. assert( state == TS_STOPPED && t.state == TS_STOPPED );
  101. clockTicks += t.clockTicks;
  102. return *this;
  103. }
  104. /*
  105. =================
  106. idTimer::operator-=
  107. =================
  108. */
  109. ID_INLINE idTimer &idTimer::operator-=( const idTimer &t ) {
  110. assert( state == TS_STOPPED && t.state == TS_STOPPED );
  111. clockTicks -= t.clockTicks;
  112. return *this;
  113. }
  114. /*
  115. =================
  116. idTimer::Start
  117. =================
  118. */
  119. ID_INLINE void idTimer::Start( void ) {
  120. assert( state == TS_STOPPED );
  121. state = TS_STARTED;
  122. start = idLib::sys->GetClockTicks();
  123. }
  124. /*
  125. =================
  126. idTimer::Stop
  127. =================
  128. */
  129. ID_INLINE void idTimer::Stop( void ) {
  130. assert( state == TS_STARTED );
  131. clockTicks += idLib::sys->GetClockTicks() - start;
  132. if ( base < 0.0 ) {
  133. InitBaseClockTicks();
  134. }
  135. if ( clockTicks > base ) {
  136. clockTicks -= base;
  137. }
  138. state = TS_STOPPED;
  139. }
  140. /*
  141. =================
  142. idTimer::Clear
  143. =================
  144. */
  145. ID_INLINE void idTimer::Clear( void ) {
  146. clockTicks = 0.0;
  147. }
  148. /*
  149. =================
  150. idTimer::ClockTicks
  151. =================
  152. */
  153. ID_INLINE double idTimer::ClockTicks( void ) const {
  154. assert( state == TS_STOPPED );
  155. return clockTicks;
  156. }
  157. /*
  158. =================
  159. idTimer::Milliseconds
  160. =================
  161. */
  162. ID_INLINE double idTimer::Milliseconds( void ) const {
  163. assert( state == TS_STOPPED );
  164. return clockTicks / ( idLib::sys->ClockTicksPerSecond() * 0.001 );
  165. }
  166. /*
  167. ===============================================================================
  168. Report of multiple named timers.
  169. ===============================================================================
  170. */
  171. class idTimerReport {
  172. public:
  173. idTimerReport( void );
  174. ~idTimerReport( void );
  175. void SetReportName( const char *name );
  176. int AddReport( const char *name );
  177. void Clear( void );
  178. void Reset( void );
  179. void PrintReport( void );
  180. void AddTime( const char *name, idTimer *time );
  181. private:
  182. idList<idTimer*>timers;
  183. idStrList names;
  184. idStr reportName;
  185. };
  186. #endif /* !__TIMER_H__ */