Profiler.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /***************************************************************************
  2. Profiler.cpp - description
  3. -------------------
  4. begin : Thu Jan 11 2001
  5. copyright : (C) 2001 by Henrik Enqvist
  6. email : henqvist@excite.com
  7. ***************************************************************************/
  8. #include <iostream>
  9. #include "Private.h"
  10. #include "Profiler.h"
  11. #include <cstdlib>
  12. #if EM_USE_SDL
  13. #include "SDL.h"
  14. #endif
  15. #if EM_USE_ALLEGRO
  16. extern volatile unsigned int g_iMSeconds;
  17. #endif
  18. Profiler* Profiler::p_Profiler = NULL;
  19. Profiler::Profiler() {
  20. for (int a=0; a<256; a++) {
  21. m_aProfile[a] = 0;
  22. }
  23. m_iCurrentProfile = 0;
  24. #if EM_USE_SDL
  25. m_iCurrentTime = m_iStart = SDL_GetTicks();
  26. #endif
  27. #if EM_USE_ALLEGRO
  28. m_iCurrentTime = m_iStart = g_iMSeconds;
  29. #endif
  30. }
  31. Profiler::~Profiler() {
  32. p_Profiler = NULL;
  33. }
  34. Profiler* Profiler::getInstance() {
  35. if (p_Profiler == NULL) {
  36. p_Profiler = new Profiler();
  37. }
  38. return p_Profiler;
  39. }
  40. void Profiler::startProfile(int i) {
  41. if (i<0 || i>255) i = 0;
  42. m_iCurrentProfile = i;
  43. #if EM_USE_SDL
  44. m_iCurrentTime = SDL_GetTicks();
  45. #endif
  46. #if EM_USE_ALLEGRO
  47. m_iCurrentTime = g_iMSeconds;
  48. #endif
  49. }
  50. void Profiler::stopProfile() {
  51. #if EM_USE_SDL
  52. m_aProfile[m_iCurrentProfile] += (SDL_GetTicks() - m_iCurrentTime);
  53. #endif
  54. #if EM_USE_ALLEGRO
  55. m_aProfile[m_iCurrentProfile] += (g_iMSeconds - m_iCurrentTime);
  56. #endif
  57. }
  58. void Profiler::printProfile() {
  59. #if EM_USE_SDL
  60. unsigned int all = SDL_GetTicks() - m_iStart;
  61. #endif
  62. #if EM_USE_ALLEGRO
  63. unsigned int all = g_iMSeconds - m_iStart;
  64. #endif
  65. unsigned int total = 0;
  66. for (int a=0; a<256; a++) {
  67. total += m_aProfile[a];
  68. }
  69. cerr << "Simple profile output *****************" << endl;
  70. cerr << "Function null " << (float)m_aProfile[0]*100.0f/total << endl;
  71. cerr << "Function align " << (float)m_aProfile[ALIGN]*100.0f/total << endl;
  72. cerr << "Function beh " << (float)m_aProfile[BEH]*100.0f/total << endl;
  73. cerr << "Function collision " << (float)m_aProfile[COLLISION]*100.0f/total << endl;
  74. cerr << "Function draw " << (float)m_aProfile[DRAW]*100.0f/total << endl;
  75. cerr << "Function glight " << (float)m_aProfile[GLIGHT]*100.0f/total << endl;
  76. cerr << "Function plight " << (float)m_aProfile[PLIGHT]*100.0f/total << endl;
  77. cerr << "Function pnormal " << (float)m_aProfile[PNORMAL]*100.0f/total << endl;
  78. cerr << "Function render " << (float)m_aProfile[RENDER]*100.0f/total << endl;
  79. cerr << "Function sound " << (float)m_aProfile[SOUND]*100.0f/total << endl;
  80. cerr << "Function signal " << (float)m_aProfile[SIG]*100.0f/total << endl;
  81. cerr << "Function trans " << (float)m_aProfile[TRANS]*100.0f/total << endl;
  82. cerr << "Function swap " << (float)m_aProfile[SWAP]*100.0f/total << endl;
  83. cerr << "Function keyboard " << (float)m_aProfile[KEY]*100.0f/total << endl;
  84. cerr << "Function render " << (float)m_aProfile[RENDER_OUT]*100.0f/total << endl;
  85. cerr << "Function swap out " << (float)m_aProfile[SWAP_OUT]*100.0f/total << endl;
  86. cerr << "Function tick out " << (float)m_aProfile[TICK_OUT]*100.0f/total << endl;
  87. cerr << "Function waiting " << (float)m_aProfile[WAIT]*100.0f/total << endl;
  88. cerr << "Unprofiled time " << (float)(all-total)*100.0f/all << " %" << endl;
  89. cerr << all <<" "<< total << endl;
  90. }