hrtimer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // hrtimer.h - originally written and placed in the public domain by Wei Dai
  2. /// \file hrtimer.h
  3. /// \brief Classes for timers
  4. #ifndef CRYPTOPP_HRTIMER_H
  5. #define CRYPTOPP_HRTIMER_H
  6. #include "config.h"
  7. #if !defined(HIGHRES_TIMER_AVAILABLE) || (defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE))
  8. #include <time.h>
  9. #endif
  10. NAMESPACE_BEGIN(CryptoPP)
  11. #ifdef HIGHRES_TIMER_AVAILABLE
  12. /// \brief TimerWord is a 64-bit word
  13. typedef word64 TimerWord;
  14. #else
  15. /// \brief TimerWord is a clock_t
  16. typedef clock_t TimerWord;
  17. #endif
  18. /// \brief Base class for timers
  19. /// \sa ThreadUserTimer, Timer
  20. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE TimerBase
  21. {
  22. public:
  23. /// \brief Unit of measure
  24. /// \details Unit selects the unit of measure as returned by functions
  25. /// ElapsedTimeAsDouble() and ElapsedTime().
  26. /// \sa ElapsedTimeAsDouble, ElapsedTime
  27. enum Unit {
  28. /// \brief Timer unit is seconds
  29. /// \details All timers support seconds
  30. SECONDS = 0,
  31. /// \brief Timer unit is milliseconds
  32. /// \details All timers support milliseconds
  33. MILLISECONDS,
  34. /// \brief Timer unit is microseconds
  35. /// \details The timer requires hardware support microseconds
  36. MICROSECONDS,
  37. /// \brief Timer unit is nanoseconds
  38. /// \details The timer requires hardware support nanoseconds
  39. NANOSECONDS
  40. };
  41. /// \brief Construct a TimerBase
  42. /// \param unit the unit of measure
  43. /// \param stuckAtZero flag
  44. TimerBase(Unit unit, bool stuckAtZero)
  45. : m_timerUnit(unit), m_stuckAtZero(stuckAtZero), m_started(false)
  46. , m_start(0), m_last(0) {}
  47. /// \brief Retrieve the current timer value
  48. /// \return the current timer value
  49. virtual TimerWord GetCurrentTimerValue() =0;
  50. /// \brief Retrieve ticks per second
  51. /// \return ticks per second
  52. /// \details TicksPerSecond() is not the timer resolution. It is a
  53. /// conversion factor into seconds.
  54. virtual TimerWord TicksPerSecond() =0;
  55. /// \brief Start the timer
  56. void StartTimer();
  57. /// \brief Retrieve the elapsed time
  58. /// \return the elapsed time as a double
  59. /// \details The return value of ElapsedTimeAsDouble() depends upon
  60. /// the Unit selected during construction of the timer. For example,
  61. /// if <tt>Unit = SECONDS</tt> and ElapsedTimeAsDouble() returns 3,
  62. /// then the timer has run for 3 seconds. If
  63. /// <tt>Unit = MILLISECONDS</tt> and ElapsedTimeAsDouble() returns
  64. /// 3000, then the timer has run for 3 seconds.
  65. /// \sa Unit, ElapsedTime
  66. double ElapsedTimeAsDouble();
  67. /// \brief Retrieve the elapsed time
  68. /// \return the elapsed time as an unsigned long
  69. /// \details The return value of ElapsedTime() depends upon the
  70. /// Unit selected during construction of the timer. For example, if
  71. /// <tt>Unit = SECONDS</tt> and ElapsedTime() returns 3, then
  72. /// the timer has run for 3 seconds. If <tt>Unit = MILLISECONDS</tt>
  73. /// and ElapsedTime() returns 3000, then the timer has run for 3
  74. /// seconds.
  75. /// \sa Unit, ElapsedTimeAsDouble
  76. unsigned long ElapsedTime();
  77. private:
  78. double ConvertTo(TimerWord t, Unit unit);
  79. Unit m_timerUnit; // HPUX workaround: m_unit is a system macro on HPUX
  80. bool m_stuckAtZero, m_started;
  81. TimerWord m_start, m_last;
  82. };
  83. /// \brief Measure CPU time spent executing instructions of this thread
  84. /// \details ThreadUserTimer requires support of the OS. On Unix-based it
  85. /// reports process time. On Windows NT or later desktops and servers it
  86. /// reports thread times with performance counter precision.. On Windows
  87. /// Phone and Windows Store it reports wall clock time with performance
  88. /// counter precision. On all others it reports wall clock time.
  89. /// \note ThreadUserTimer only works correctly on Windows NT or later
  90. /// desktops and servers.
  91. /// \sa Timer
  92. class ThreadUserTimer : public TimerBase
  93. {
  94. public:
  95. /// \brief Construct a ThreadUserTimer
  96. /// \param unit the unit of measure
  97. /// \param stuckAtZero flag
  98. ThreadUserTimer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
  99. TimerWord GetCurrentTimerValue();
  100. TimerWord TicksPerSecond();
  101. };
  102. /// \brief High resolution timer
  103. /// \sa ThreadUserTimer
  104. class CRYPTOPP_DLL Timer : public TimerBase
  105. {
  106. public:
  107. /// \brief Construct a Timer
  108. /// \param unit the unit of measure
  109. /// \param stuckAtZero flag
  110. Timer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
  111. TimerWord GetCurrentTimerValue();
  112. TimerWord TicksPerSecond();
  113. };
  114. NAMESPACE_END
  115. #endif