Crypto++  5.6.3
Free C++ class library of cryptographic schemes
hrtimer.h
1 #ifndef CRYPTOPP_HRTIMER_H
2 #define CRYPTOPP_HRTIMER_H
3 
4 #include "config.h"
5 
6 #if !defined(HIGHRES_TIMER_AVAILABLE) || (defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE))
7 #include <time.h>
8 #endif
9 
10 NAMESPACE_BEGIN(CryptoPP)
11 
12 #ifdef HIGHRES_TIMER_AVAILABLE
13  typedef word64 TimerWord;
14 #else
15  typedef clock_t TimerWord;
16 #endif
17 
18 //! \class TimerBase
19 //! \brief Base class for timers
20 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE TimerBase
21 {
22 public:
23  enum Unit {SECONDS = 0, MILLISECONDS, MICROSECONDS, NANOSECONDS};
24  TimerBase(Unit unit, bool stuckAtZero)
25  : m_timerUnit(unit), m_stuckAtZero(stuckAtZero), m_started(false)
26  , m_start(0), m_last(0) {}
27 
28  virtual TimerWord GetCurrentTimerValue() =0; // GetCurrentTime is a macro in MSVC 6.0
29  virtual TimerWord TicksPerSecond() =0; // this is not the resolution, just a conversion factor into seconds
30 
31  void StartTimer();
32  double ElapsedTimeAsDouble();
33  unsigned long ElapsedTime();
34 
35 private:
36  double ConvertTo(TimerWord t, Unit unit);
37 
38  Unit m_timerUnit; // HPUX workaround: m_unit is a system macro on HPUX
39  bool m_stuckAtZero, m_started;
40  TimerWord m_start, m_last;
41 };
42 
43 //! \class ThreadUserTimer
44 //! \brief Measure CPU time spent executing instructions of this thread (if supported by OS)
45 //! \note ThreadUserTimer only works correctly on Windows NT or later desktops and servers.
46 //! On Unix-based it reports process time. On Windows Phone and Windows Store it reports wall
47 //! clock time with performance counter precision. On all others it reports wall clock time.
48 class ThreadUserTimer : public TimerBase
49 {
50 public:
51  ThreadUserTimer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
52  TimerWord GetCurrentTimerValue();
53  TimerWord TicksPerSecond();
54 };
55 
56 //! high resolution timer
57 class CRYPTOPP_DLL Timer : public TimerBase
58 {
59 public:
60  Timer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
61  TimerWord GetCurrentTimerValue();
62  TimerWord TicksPerSecond();
63 };
64 
65 NAMESPACE_END
66 
67 #endif
high resolution timer
Definition: hrtimer.h:57
Library configuration file.
Crypto++ library namespace.
Measure CPU time spent executing instructions of this thread (if supported by OS) ...
Definition: hrtimer.h:48
Base class for timers.
Definition: hrtimer.h:20