Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.2 KiB

  1. //
  2. // The CPerfTimer class can be used to time operations to an accuracy of
  3. // around 1 microsecond.
  4. //
  5. // "#define TIMING 1" to turn Timer macros on.
  6. #undef DeclarePerfTimerOn
  7. #undef DeclarePerfTimerOff
  8. #undef PerfTimerOn_
  9. #undef PerfTimerOff_
  10. #undef PerfTimerResetTotal_
  11. #undef PerfTimerDumpTotal_
  12. #undef PerfTimerReset_
  13. #undef PerfTimerDump_
  14. #undef DeclarePerfTimer
  15. #undef PerfTimerResetTotal
  16. #undef PerfTimerDumpTotal
  17. #undef PerfTimerReset
  18. #undef PerfTimerDump
  19. #if TIMING
  20. // #pragma message(__FILE__ " : warning : PerfTimer macros enabled")
  21. #define DeclarePerfTimerOn(t, sz) CPerfTimer t(sz)
  22. #define DeclarePerfTimerOff(t, sz) CPerfTimer t(sz, FALSE)
  23. #define PerfTimerOn_(perf) perf.On()
  24. #define PerfTimerOff_(perf) perf.Off()
  25. #define PerfTimerResetTotal_(perf) perf.ResetTotal()
  26. #define PerfTimerDumpTotal_(perf,sz) perf.DumpTotal(sz)
  27. #define PerfTimerReset_(perf) perf.Reset()
  28. #define PerfTimerDump_(perf,sz) perf.Dump(sz)
  29. #define DeclarePerfTimer(sz) CPerfTimer perf(sz)
  30. #define PerfTimerResetTotal() PerfTimerResetTotal_(perf)
  31. #define PerfTimerDumpTotal(sz) PerfTimerDumpTotal_(perf, sz)
  32. #define PerfTimerReset() PerfTimerReset_(perf)
  33. #define PerfTimerDump(sz) PerfTimerDump_(perf,sz)
  34. class CPerfTimer
  35. {
  36. public:
  37. CPerfTimer(const char *sz)
  38. {
  39. Init(sz, TRUE);
  40. }
  41. CPerfTimer(const char *sz, BOOL fEnabled)
  42. {
  43. Init(sz, fEnabled);
  44. }
  45. void Init(const char *sz, BOOL fEnabled)
  46. {
  47. m_sz = sz;
  48. m_cEnabled = fEnabled ? 1 : 0;
  49. m_ulTimeStart = 0;
  50. m_ulTimeTotal = 0;
  51. QueryPerformanceFrequency((LARGE_INTEGER *)&m_ulFreq);
  52. }
  53. void On(void)
  54. {
  55. m_cEnabled++;
  56. }
  57. void Off()
  58. {
  59. m_cEnabled--;
  60. }
  61. void ResetTotal(void)
  62. {
  63. m_ulTimeTotal = 0;
  64. }
  65. void Reset(void)
  66. {
  67. QueryPerformanceCounter((LARGE_INTEGER *) &m_ulTimeStart);
  68. }
  69. void Dump(const char *sz)
  70. {
  71. _int64 ulTime;
  72. QueryPerformanceCounter((LARGE_INTEGER *) &ulTime);
  73. ulTime -= m_ulTimeStart;
  74. m_ulTimeTotal += ulTime;
  75. DumpTime(sz, ulTime);
  76. Reset();
  77. }
  78. void DumpTotal(const char *sz)
  79. {
  80. DumpTime(sz, m_ulTimeTotal);
  81. }
  82. void DumpTime(const char *sz, _int64 ulTime)
  83. {
  84. if (m_cEnabled > 0)
  85. {
  86. _int64 nSecs = (ulTime / m_ulFreq);
  87. _int64 nMilliSecs = ((ulTime * 1000L) / m_ulFreq) % 1000;
  88. _int64 nMicroSecs = ((ulTime * 1000000L) / m_ulFreq) % 1000;
  89. TRACE("%s %s: %lu s %lu.%3.3lu ms\n", m_sz, sz,
  90. (ULONG) nSecs, (ULONG) nMilliSecs, (ULONG) nMicroSecs);
  91. }
  92. }
  93. protected:
  94. const char * m_sz;
  95. _int64 m_ulTimeStart;
  96. _int64 m_ulTimeTotal;
  97. _int64 m_ulFreq;
  98. int m_cEnabled;
  99. };
  100. #else
  101. #define DeclarePerfTimerOn(sz,t)
  102. #define DeclarePerfTimerOff(sz,t)
  103. #define PerfTimerOn_(perf)
  104. #define PerfTimerOff_(perf)
  105. #define PerfTimerResetTotal_(perf)
  106. #define PerfTimerDumpTotal_(perf,sz)
  107. #define PerfTimerReset_(perf)
  108. #define PerfTimerDump_(perf,sz)
  109. #define DeclarePerfTimer(sz)
  110. #define PerfTimerResetTotal()
  111. #define PerfTimerDumpTotal(sz)
  112. #define PerfTimerReset()
  113. #define PerfTimerDump(sz)
  114. #endif