lzham_timer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // File: lzham_timer.h
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #pragma once
  4. namespace lzham
  5. {
  6. typedef unsigned long long timer_ticks;
  7. class lzham_timer
  8. {
  9. public:
  10. lzham_timer();
  11. lzham_timer(timer_ticks start_ticks);
  12. void start();
  13. void start(timer_ticks start_ticks);
  14. void stop();
  15. double get_elapsed_secs() const;
  16. inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; }
  17. timer_ticks get_elapsed_us() const;
  18. static void init();
  19. static inline timer_ticks get_ticks_per_sec() { return g_freq; }
  20. static timer_ticks get_init_ticks();
  21. static timer_ticks get_ticks();
  22. static double ticks_to_secs(timer_ticks ticks);
  23. static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; }
  24. static inline double get_secs() { return ticks_to_secs(get_ticks()); }
  25. static inline double get_ms() { return ticks_to_ms(get_ticks()); }
  26. private:
  27. static timer_ticks g_init_ticks;
  28. static timer_ticks g_freq;
  29. static double g_inv_freq;
  30. timer_ticks m_start_time;
  31. timer_ticks m_stop_time;
  32. bool m_started : 1;
  33. bool m_stopped : 1;
  34. };
  35. enum var_args_t { cVarArgs };
  36. #if LZHAM_PERF_SECTIONS
  37. class scoped_perf_section
  38. {
  39. public:
  40. inline scoped_perf_section() :
  41. m_start_ticks(lzham_timer::get_ticks())
  42. {
  43. m_name[0] = '?';
  44. m_name[1] = '\0';
  45. }
  46. inline scoped_perf_section(const char *pName) :
  47. m_start_ticks(lzham_timer::get_ticks())
  48. {
  49. strcpy_s(m_name, pName);
  50. lzham_buffered_printf("Thread: 0x%08X, BEGIN Time: %3.3fms, Section: %s\n", GetCurrentThreadId(), lzham_timer::ticks_to_ms(m_start_ticks), m_name);
  51. }
  52. inline scoped_perf_section(var_args_t, const char *pName, ...) :
  53. m_start_ticks(lzham_timer::get_ticks())
  54. {
  55. va_list args;
  56. va_start(args, pName);
  57. vsprintf_s(m_name, sizeof(m_name), pName, args);
  58. va_end(args);
  59. lzham_buffered_printf("Thread: 0x%08X, BEGIN Time: %3.3fms, Section: %s\n", GetCurrentThreadId(), lzham_timer::ticks_to_ms(m_start_ticks), m_name);
  60. }
  61. inline ~scoped_perf_section()
  62. {
  63. double end_ms = lzham_timer::get_ms();
  64. double start_ms = lzham_timer::ticks_to_ms(m_start_ticks);
  65. lzham_buffered_printf("Thread: 0x%08X, END Time: %3.3fms, Total: %3.3fms, Section: %s\n", GetCurrentThreadId(), end_ms, end_ms - start_ms, m_name);
  66. }
  67. private:
  68. char m_name[64];
  69. timer_ticks m_start_ticks;
  70. };
  71. #else
  72. class scoped_perf_section
  73. {
  74. public:
  75. inline scoped_perf_section() { }
  76. inline scoped_perf_section(const char *pName) { (void)pName; }
  77. inline scoped_perf_section(var_args_t, const char *pName, ...) { (void)pName; }
  78. };
  79. #endif // LZHAM_PERF_SECTIONS
  80. } // namespace lzham