9 #if defined(CRYPTOPP_WIN32_AVAILABLE)
11 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
13 #include <sys/times.h>
19 NAMESPACE_BEGIN(CryptoPP)
21 #ifndef CRYPTOPP_IMPORTS
23 double TimerBase::ConvertTo(TimerWord t, Unit unit)
25 static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
27 assert(unit <
sizeof(unitsPerSecondTable) /
sizeof(unitsPerSecondTable[0]));
28 return (
double)CRYPTOPP_VC6_INT64 t * unitsPerSecondTable[unit] / CRYPTOPP_VC6_INT64 TicksPerSecond();
31 void TimerBase::StartTimer()
33 m_last = m_start = GetCurrentTimerValue();
37 double TimerBase::ElapsedTimeAsDouble()
44 TimerWord now = GetCurrentTimerValue();
47 return ConvertTo(m_last - m_start, m_timerUnit);
54 unsigned long TimerBase::ElapsedTime()
56 double elapsed = ElapsedTimeAsDouble();
57 assert(elapsed <= ULONG_MAX);
58 return (
unsigned long)elapsed;
61 TimerWord Timer::GetCurrentTimerValue()
63 #if defined(CRYPTOPP_WIN32_AVAILABLE)
65 if (!QueryPerformanceCounter(&now))
68 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
70 gettimeofday(&now, NULL);
71 return (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
78 TimerWord Timer::TicksPerSecond()
80 #if defined(CRYPTOPP_WIN32_AVAILABLE)
81 static LARGE_INTEGER freq = {0};
82 if (freq.QuadPart == 0)
84 if (!QueryPerformanceFrequency(&freq))
88 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
91 return CLOCKS_PER_SEC;
95 #endif // #ifndef CRYPTOPP_IMPORTS
97 TimerWord ThreadUserTimer::GetCurrentTimerValue()
99 #if defined(CRYPTOPP_WIN32_AVAILABLE)
100 static bool getCurrentThreadImplemented =
true;
101 if (getCurrentThreadImplemented)
103 FILETIME now, ignored;
104 if (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now))
106 DWORD lastError = GetLastError();
107 if (lastError == ERROR_CALL_NOT_IMPLEMENTED)
109 getCurrentThreadImplemented =
false;
110 goto GetCurrentThreadNotImplemented;
114 return now.dwLowDateTime + ((TimerWord)now.dwHighDateTime << 32);
116 GetCurrentThreadNotImplemented:
117 return (TimerWord)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
118 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
121 return now.tms_utime;
127 TimerWord ThreadUserTimer::TicksPerSecond()
129 #if defined(CRYPTOPP_WIN32_AVAILABLE)
131 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
132 static const long ticksPerSecond = sysconf(_SC_CLK_TCK);
133 return ticksPerSecond;
135 return CLOCKS_PER_SEC;