Timer.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  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. #include "precompiled.h"
  21. #pragma hdrstop
  22. double idTimer::base = -1.0;
  23. /*
  24. =================
  25. idTimer::InitBaseClockTicks
  26. =================
  27. */
  28. void idTimer::InitBaseClockTicks() const {
  29. idTimer timer;
  30. double ct, b;
  31. int i;
  32. base = 0.0;
  33. b = -1.0;
  34. for ( i = 0; i < 1000; i++ ) {
  35. timer.Clear();
  36. timer.Start();
  37. timer.Stop();
  38. ct = timer.ClockTicks();
  39. if ( b < 0.0 || ct < b ) {
  40. b = ct;
  41. }
  42. }
  43. base = b;
  44. }
  45. /*
  46. =================
  47. idTimerReport::idTimerReport
  48. =================
  49. */
  50. idTimerReport::idTimerReport() {
  51. }
  52. /*
  53. =================
  54. idTimerReport::SetReportName
  55. =================
  56. */
  57. void idTimerReport::SetReportName( const char *name ) {
  58. reportName = ( name ) ? name : "Timer Report";
  59. }
  60. /*
  61. =================
  62. idTimerReport::~idTimerReport
  63. =================
  64. */
  65. idTimerReport::~idTimerReport() {
  66. Clear();
  67. }
  68. /*
  69. =================
  70. idTimerReport::AddReport
  71. =================
  72. */
  73. int idTimerReport::AddReport( const char *name ) {
  74. if ( name && *name ) {
  75. names.Append( name );
  76. return timers.Append( new (TAG_IDLIB) idTimer() );
  77. }
  78. return -1;
  79. }
  80. /*
  81. =================
  82. idTimerReport::Clear
  83. =================
  84. */
  85. void idTimerReport::Clear() {
  86. timers.DeleteContents( true );
  87. names.Clear();
  88. reportName.Clear();
  89. }
  90. /*
  91. =================
  92. idTimerReport::Reset
  93. =================
  94. */
  95. void idTimerReport::Reset() {
  96. assert ( timers.Num() == names.Num() );
  97. for ( int i = 0; i < timers.Num(); i++ ) {
  98. timers[i]->Clear();
  99. }
  100. }
  101. /*
  102. =================
  103. idTimerReport::AddTime
  104. =================
  105. */
  106. void idTimerReport::AddTime( const char *name, idTimer *time ) {
  107. assert ( timers.Num() == names.Num() );
  108. int i;
  109. for ( i = 0; i < names.Num(); i++ ) {
  110. if ( names[i].Icmp( name ) == 0 ) {
  111. *timers[i] += *time;
  112. break;
  113. }
  114. }
  115. if ( i == names.Num() ) {
  116. int index = AddReport( name );
  117. if ( index >= 0 ) {
  118. timers[index]->Clear();
  119. *timers[index] += *time;
  120. }
  121. }
  122. }
  123. /*
  124. =================
  125. idTimerReport::PrintReport
  126. =================
  127. */
  128. void idTimerReport::PrintReport() {
  129. assert( timers.Num() == names.Num() );
  130. idLib::common->Printf( "Timing Report for %s\n", reportName.c_str() );
  131. idLib::common->Printf( "-------------------------------\n" );
  132. float total = 0.0f;
  133. for ( int i = 0; i < names.Num(); i++ ) {
  134. idLib::common->Printf( "%s consumed %5.2f seconds\n", names[i].c_str(), timers[i]->Milliseconds() * 0.001f );
  135. total += timers[i]->Milliseconds();
  136. }
  137. idLib::common->Printf( "Total time for report %s was %5.2f\n\n", reportName.c_str(), total * 0.001f );
  138. }