DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Timer.cpp (4070B)


      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 #include "precompiled.h"
     30 #pragma hdrstop
     31 
     32 double idTimer::base = -1.0;
     33 
     34 
     35 /*
     36 =================
     37 idTimer::InitBaseClockTicks
     38 =================
     39 */
     40 void idTimer::InitBaseClockTicks() const {
     41 	idTimer timer;
     42 	double ct, b;
     43 	int i;
     44 
     45 	base = 0.0;
     46 	b = -1.0;
     47 	for ( i = 0; i < 1000; i++ ) {
     48 		timer.Clear();
     49 		timer.Start();
     50 		timer.Stop();
     51 		ct = timer.ClockTicks();
     52 		if ( b < 0.0 || ct < b ) {
     53 			b = ct;
     54 		}
     55 	}
     56 	base = b;
     57 }
     58 
     59 
     60 /*
     61 =================
     62 idTimerReport::idTimerReport
     63 =================
     64 */
     65 idTimerReport::idTimerReport() {
     66 }
     67 
     68 /*
     69 =================
     70 idTimerReport::SetReportName
     71 =================
     72 */
     73 void idTimerReport::SetReportName( const char *name ) {
     74 	reportName = ( name ) ? name : "Timer Report";
     75 }
     76 
     77 /*
     78 =================
     79 idTimerReport::~idTimerReport
     80 =================
     81 */
     82 idTimerReport::~idTimerReport() {
     83 	Clear();
     84 }
     85 
     86 /*
     87 =================
     88 idTimerReport::AddReport
     89 =================
     90 */
     91 int idTimerReport::AddReport( const char *name ) {
     92 	if ( name && *name ) {
     93 		names.Append( name );
     94 		return timers.Append( new (TAG_IDLIB) idTimer() );
     95 	}
     96 	return -1;
     97 }
     98 
     99 /*
    100 =================
    101 idTimerReport::Clear
    102 =================
    103 */
    104 void idTimerReport::Clear() {
    105 	timers.DeleteContents( true );
    106 	names.Clear();
    107 	reportName.Clear();
    108 }
    109 
    110 /*
    111 =================
    112 idTimerReport::Reset
    113 =================
    114 */
    115 void idTimerReport::Reset() {
    116 	assert ( timers.Num() == names.Num() );
    117 	for ( int i = 0; i < timers.Num(); i++ ) {
    118 		timers[i]->Clear();
    119 	}
    120 }
    121 
    122 /*
    123 =================
    124 idTimerReport::AddTime
    125 =================
    126 */
    127 void idTimerReport::AddTime( const char *name, idTimer *time ) {
    128 	assert ( timers.Num() == names.Num() );
    129 	int i;
    130 	for ( i = 0; i < names.Num(); i++ ) {
    131 		if ( names[i].Icmp( name ) == 0 ) {
    132 			*timers[i] += *time;
    133 			break;
    134 		}
    135 	}
    136 	if ( i == names.Num() ) {
    137 		int index = AddReport( name );
    138 		if ( index >= 0 ) {
    139 			timers[index]->Clear();
    140 			*timers[index] += *time;
    141 		}
    142 	}
    143 }
    144 
    145 /*
    146 =================
    147 idTimerReport::PrintReport
    148 =================
    149 */
    150 void idTimerReport::PrintReport() {
    151 	assert( timers.Num() == names.Num() );
    152 	idLib::common->Printf( "Timing Report for %s\n", reportName.c_str() );
    153 	idLib::common->Printf( "-------------------------------\n" );
    154 	float total = 0.0f;
    155 	for ( int i = 0; i < names.Num(); i++ ) {
    156 		idLib::common->Printf( "%s consumed %5.2f seconds\n", names[i].c_str(), timers[i]->Milliseconds() * 0.001f );
    157 		total += timers[i]->Milliseconds();
    158 	}
    159 	idLib::common->Printf( "Total time for report %s was %5.2f\n\n", reportName.c_str(), total * 0.001f );
    160 }