Guest User

Untitled

a guest
May 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #ifndef TINY_PROFILER
  2. #define TINY_PROFILER 1
  3.  
  4. // Tiny Profiler is a lightweight tool for tracking and logging multiple tasks. I use it to
  5. // see which parts of my OpenGL scene graph take the longest to render.
  6. //
  7. // Because logging is relatively expensive, the TinyProfilerLog() function only logs every
  8. // nth time it is called, where n is TINY_PROFILER_LOG_INTERVAL. Set this to 1 to log every time.
  9. //
  10. // TINY_PROFILER_COUNT is the maximum number of active profilers. There is no bounds-checking,
  11. // so be very careful that this number is large enough.
  12. //
  13. // Usage:
  14. //
  15. // #include "TinyProfiler.h"
  16. //
  17. // TinyProfilerStart(0);
  18. //
  19. // // task 0 goes here
  20. //
  21. // TinyProfilerStop(0);
  22. //
  23. // TinyProfilerStart(1);
  24. //
  25. // // task 1 goes here
  26. //
  27. // TinyProfilerStop(1);
  28. //
  29. // TinyProfilerLog();
  30.  
  31. #define TINY_PROFILER_SHOULD_RUN 1
  32.  
  33. //Set these constants to match your own profiling needs
  34.  
  35. #define TINY_PROFILER_LOG_INTERVAL 300
  36. #define TINY_PROFILER_COUNT 32
  37. #define TINY_PROFILER_TIMER CFAbsoluteTimeGetCurrent()
  38. #define TINY_PROFILER_TIME_TYPE NSTimeInterval
  39. #define TINY_PROFILER_LOG(...) printf("%d, %d, %f5\n", __VA_ARGS__)
  40.  
  41. typedef struct
  42. {
  43. TINY_PROFILER_TIME_TYPE startTime;
  44. TINY_PROFILER_TIME_TYPE totalTime;
  45. int runCount;
  46. }
  47. TinyProfiler;
  48.  
  49. static TinyProfiler tinyProfilers[TINY_PROFILER_COUNT];
  50. static int tinyProfilerLogCounter = 0;
  51.  
  52. static inline void TinyProfilerStart(int profileIndex)
  53. {
  54. #ifdef TINY_PROFILER_SHOULD_RUN
  55.  
  56. tinyProfilers[profileIndex].startTime = TINY_PROFILER_TIMER;
  57.  
  58. #endif
  59. }
  60.  
  61. static inline void TinyProfilerStop(int profileIndex)
  62. {
  63. #ifdef TINY_PROFILER_SHOULD_RUN
  64.  
  65. tinyProfilers[profileIndex].totalTime = TINY_PROFILER_TIMER - tinyProfilers[profileIndex].startTime;
  66. tinyProfilers[profileIndex].runCount++;
  67.  
  68. #endif
  69. }
  70.  
  71. static inline void TinyProfilerLog()
  72. {
  73. #ifdef TINY_PROFILER_SHOULD_RUN
  74.  
  75. if(++tinyProfilerLogCounter != TINY_PROFILER_LOG_INTERVAL) { return; }
  76.  
  77. for(int profileIndex = 0; profileIndex < TINY_PROFILER_COUNT; profileIndex++)
  78. {
  79. if(!tinyProfilers[profileIndex].runCount) { continue; }
  80.  
  81. TINY_PROFILER_LOG(profileIndex, tinyProfilers[profileIndex].runCount, tinyProfilers[profileIndex].totalTime);
  82.  
  83. tinyProfilers[profileIndex].startTime = 0;
  84. tinyProfilers[profileIndex].totalTime = 0;
  85. tinyProfilers[profileIndex].runCount = 0;
  86. }
  87.  
  88. tinyProfilerLogCounter = 0;
  89.  
  90. #endif
  91. }
  92.  
  93. #endif
  94.  
  95. // ----------------------------------------------------------------------------
  96. // "THE BEER-WARE LICENSE" (Revision 42):
  97. // Joel Bernstein wrote this file. As long as you retain this notice you
  98. // can do whatever you want with this stuff. If we meet some day, and you think
  99. // this stuff is worth it, you can buy me a beer in return.
  100. // ----------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment