Crypto++  8.0
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 /// \brief Base class for timers
19 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE TimerBase
20 {
21 public:
22  enum Unit {SECONDS = 0, MILLISECONDS, MICROSECONDS, NANOSECONDS};
23  TimerBase(Unit unit, bool stuckAtZero)
24  : m_timerUnit(unit), m_stuckAtZero(stuckAtZero), m_started(false)
25  , m_start(0), m_last(0) {}
26 
27  virtual TimerWord GetCurrentTimerValue() =0; // GetCurrentTime is a macro in MSVC 6.0
28  virtual TimerWord TicksPerSecond() =0; // this is not the resolution, just a conversion factor into seconds
29 
30  void StartTimer();
31  double ElapsedTimeAsDouble();
32  unsigned long ElapsedTime();
33 
34 private:
35  double ConvertTo(TimerWord t, Unit unit);
36 
37  Unit m_timerUnit; // HPUX workaround: m_unit is a system macro on HPUX
38  bool m_stuckAtZero, m_started;
39  TimerWord m_start, m_last;
40 };
41 
42 /// \brief Measure CPU time spent executing instructions of this thread (if supported by OS)
43 /// \note ThreadUserTimer only works correctly on Windows NT or later desktops and servers.
44 /// On Unix-based it reports process time. On Windows Phone and Windows Store it reports wall
45 /// clock time with performance counter precision. On all others it reports wall clock time.
46 class ThreadUserTimer : public TimerBase
47 {
48 public:
49  ThreadUserTimer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
50  TimerWord GetCurrentTimerValue();
51  TimerWord TicksPerSecond();
52 };
53 
54 /// high resolution timer
55 class CRYPTOPP_DLL Timer : public TimerBase
56 {
57 public:
58  Timer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
59  TimerWord GetCurrentTimerValue();
60  TimerWord TicksPerSecond();
61 };
62 
63 NAMESPACE_END
64 
65 #endif
high resolution timer
Definition: hrtimer.h:55
Library configuration file.
Crypto++ library namespace.
Measure CPU time spent executing instructions of this thread (if supported by OS) ...
Definition: hrtimer.h:46
Base class for timers.
Definition: hrtimer.h:19