profilerLib.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include <intrin.h>
  3. #include <Windows.h>
  4. ///////////////////////////////////////////
  5. //https://github.com/meemknight/profilerLib
  6. //do not remove this notice
  7. //(c) Luta Vlad
  8. ///////////////////////////////////////////
  9. //set this to true to remove the implementation
  10. //usefull to quickly remove debug and profile cod and to port to
  11. //other platforms
  12. #define PROFILER_LIB_REMOVE_IMPLEMENTATION 0
  13. namespace PL
  14. {
  15. struct ProfileRezults
  16. {
  17. float timeSeconds;
  18. unsigned int cpuClocks;
  19. };
  20. #if !PROFILER_LIB_REMOVE_IMPLEMENTATION
  21. struct PerfFreqvency
  22. {
  23. PerfFreqvency()
  24. {
  25. QueryPerformanceFrequency(&perfFreq);
  26. }
  27. LARGE_INTEGER perfFreq;
  28. };
  29. const static PerfFreqvency freq;
  30. struct Profiler
  31. {
  32. LARGE_INTEGER startTime = {};
  33. __int64 cycleCount = {};
  34. void start()
  35. {
  36. QueryPerformanceCounter(&startTime);
  37. cycleCount = __rdtsc();
  38. }
  39. ProfileRezults end()
  40. {
  41. __int64 endCycleCount = __rdtsc();
  42. LARGE_INTEGER endTime;
  43. QueryPerformanceCounter(&endTime);
  44. cycleCount = endCycleCount - cycleCount;
  45. startTime.QuadPart = endTime.QuadPart - startTime.QuadPart;
  46. ProfileRezults r = {};
  47. r.timeSeconds = (float)startTime.QuadPart / (float)freq.perfFreq.QuadPart;
  48. r.cpuClocks = cycleCount;
  49. return r;
  50. }
  51. };
  52. const int AverageProfilerMaxTests = 200;
  53. struct AverageProfiler
  54. {
  55. ProfileRezults rezults[AverageProfilerMaxTests];
  56. int index = 0;
  57. Profiler profiler;
  58. void start()
  59. {
  60. profiler.start();
  61. }
  62. ProfileRezults end()
  63. {
  64. auto r = profiler.end();
  65. if(index < AverageProfilerMaxTests)
  66. {
  67. rezults[index] = r;
  68. index++;
  69. }
  70. return r;
  71. }
  72. ProfileRezults getAverageNoResetData()
  73. {
  74. if (index == 0)
  75. {
  76. return { 0,0 };
  77. }
  78. long double time = 0;
  79. unsigned long cpuTime = 0;
  80. for(int i=0;i<index;i++)
  81. {
  82. time += rezults[i].timeSeconds;
  83. cpuTime += rezults[i].cpuClocks;
  84. }
  85. return { (float)(time / index), cpuTime /index };
  86. }
  87. void resetData()
  88. {
  89. index = 0;
  90. }
  91. ProfileRezults getAverageAndResetData()
  92. {
  93. auto r = getAverageNoResetData();
  94. resetData();
  95. return r;
  96. }
  97. };
  98. #else
  99. struct Profiler
  100. {
  101. void start()
  102. {
  103. }
  104. ProfileRezults end()
  105. {
  106. return {};
  107. }
  108. };
  109. struct AverageProfiler
  110. {
  111. void start()
  112. {
  113. }
  114. ProfileRezults end()
  115. {
  116. return {};
  117. }
  118. ProfileRezults getAverageNoResetData()
  119. {
  120. return {};
  121. }
  122. void resetData()
  123. {
  124. }
  125. ProfileRezults getAverageAndResetData()
  126. {
  127. return {};
  128. }
  129. };
  130. #endif
  131. };