Timer.h (5275B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition 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 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 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. 25 26 =========================================================================== 27 */ 28 29 #ifndef __TIMER_H__ 30 #define __TIMER_H__ 31 32 /* 33 =============================================================================== 34 35 Clock tick counter. Should only be used for profiling. 36 37 =============================================================================== 38 */ 39 40 class idTimer { 41 public: 42 idTimer(); 43 idTimer( double clockTicks ); 44 ~idTimer(); 45 46 idTimer operator+( const idTimer &t ) const; 47 idTimer operator-( const idTimer &t ) const; 48 idTimer & operator+=( const idTimer &t ); 49 idTimer & operator-=( const idTimer &t ); 50 51 void Start(); 52 void Stop(); 53 void Clear(); 54 double ClockTicks() const; 55 double Milliseconds() const; 56 57 private: 58 static double base; 59 enum { 60 TS_STARTED, 61 TS_STOPPED 62 } state; 63 double start; 64 double clockTicks; 65 66 void InitBaseClockTicks() const; 67 }; 68 69 /* 70 ================= 71 idTimer::idTimer 72 ================= 73 */ 74 ID_INLINE idTimer::idTimer() { 75 state = TS_STOPPED; 76 clockTicks = 0.0; 77 } 78 79 /* 80 ================= 81 idTimer::idTimer 82 ================= 83 */ 84 ID_INLINE idTimer::idTimer( double _clockTicks ) { 85 state = TS_STOPPED; 86 clockTicks = _clockTicks; 87 } 88 89 /* 90 ================= 91 idTimer::~idTimer 92 ================= 93 */ 94 ID_INLINE idTimer::~idTimer() { 95 } 96 97 /* 98 ================= 99 idTimer::operator+ 100 ================= 101 */ 102 ID_INLINE idTimer idTimer::operator+( const idTimer &t ) const { 103 assert( state == TS_STOPPED && t.state == TS_STOPPED ); 104 return idTimer( clockTicks + t.clockTicks ); 105 } 106 107 /* 108 ================= 109 idTimer::operator- 110 ================= 111 */ 112 ID_INLINE idTimer idTimer::operator-( const idTimer &t ) const { 113 assert( state == TS_STOPPED && t.state == TS_STOPPED ); 114 return idTimer( clockTicks - t.clockTicks ); 115 } 116 117 /* 118 ================= 119 idTimer::operator+= 120 ================= 121 */ 122 ID_INLINE idTimer &idTimer::operator+=( const idTimer &t ) { 123 assert( state == TS_STOPPED && t.state == TS_STOPPED ); 124 clockTicks += t.clockTicks; 125 return *this; 126 } 127 128 /* 129 ================= 130 idTimer::operator-= 131 ================= 132 */ 133 ID_INLINE idTimer &idTimer::operator-=( const idTimer &t ) { 134 assert( state == TS_STOPPED && t.state == TS_STOPPED ); 135 clockTicks -= t.clockTicks; 136 return *this; 137 } 138 139 /* 140 ================= 141 idTimer::Start 142 ================= 143 */ 144 ID_INLINE void idTimer::Start() { 145 assert( state == TS_STOPPED ); 146 state = TS_STARTED; 147 start = idLib::sys->GetClockTicks(); 148 } 149 150 /* 151 ================= 152 idTimer::Stop 153 ================= 154 */ 155 ID_INLINE void idTimer::Stop() { 156 assert( state == TS_STARTED ); 157 clockTicks += idLib::sys->GetClockTicks() - start; 158 if ( base < 0.0 ) { 159 InitBaseClockTicks(); 160 } 161 if ( clockTicks > base ) { 162 clockTicks -= base; 163 } 164 state = TS_STOPPED; 165 } 166 167 /* 168 ================= 169 idTimer::Clear 170 ================= 171 */ 172 ID_INLINE void idTimer::Clear() { 173 clockTicks = 0.0; 174 } 175 176 /* 177 ================= 178 idTimer::ClockTicks 179 ================= 180 */ 181 ID_INLINE double idTimer::ClockTicks() const { 182 assert( state == TS_STOPPED ); 183 return clockTicks; 184 } 185 186 /* 187 ================= 188 idTimer::Milliseconds 189 ================= 190 */ 191 ID_INLINE double idTimer::Milliseconds() const { 192 assert( state == TS_STOPPED ); 193 return clockTicks / ( idLib::sys->ClockTicksPerSecond() * 0.001 ); 194 } 195 196 197 /* 198 =============================================================================== 199 200 Report of multiple named timers. 201 202 =============================================================================== 203 */ 204 205 class idTimerReport { 206 public: 207 idTimerReport(); 208 ~idTimerReport(); 209 210 void SetReportName( const char *name ); 211 int AddReport( const char *name ); 212 void Clear(); 213 void Reset(); 214 void PrintReport(); 215 void AddTime( const char *name, idTimer *time ); 216 217 private: 218 idList<idTimer*>timers; 219 idStrList names; 220 idStr reportName; 221 }; 222 223 #endif /* !__TIMER_H__ */