9 #if defined(CRYPTOPP_WIN32_AVAILABLE) 11 #elif defined(CRYPTOPP_UNIX_AVAILABLE) 13 #include <sys/times.h> 21 #ifndef CRYPTOPP_IMPORTS 23 double TimerBase::ConvertTo(TimerWord t, Unit unit)
25 static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
28 assert(static_cast<unsigned int>(unit) <
COUNTOF(unitsPerSecondTable));
29 return (
double)CRYPTOPP_VC6_INT64 t * unitsPerSecondTable[unit] / CRYPTOPP_VC6_INT64 TicksPerSecond();
32 void TimerBase::StartTimer()
34 m_last = m_start = GetCurrentTimerValue();
38 double TimerBase::ElapsedTimeAsDouble()
45 TimerWord now = GetCurrentTimerValue();
48 return ConvertTo(m_last - m_start, m_timerUnit);
55 unsigned long TimerBase::ElapsedTime()
57 double elapsed = ElapsedTimeAsDouble();
58 assert(elapsed <= ULONG_MAX);
59 return (
unsigned long)elapsed;
62 TimerWord Timer::GetCurrentTimerValue()
64 #if defined(CRYPTOPP_WIN32_AVAILABLE) 66 LARGE_INTEGER now = {0,0};
67 if (!QueryPerformanceCounter(&now))
70 #elif defined(CRYPTOPP_UNIX_AVAILABLE) 72 gettimeofday(&now, NULL);
73 return (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
80 TimerWord Timer::TicksPerSecond()
82 #if defined(CRYPTOPP_WIN32_AVAILABLE) 84 static LARGE_INTEGER freq = {0,0};
85 if (freq.QuadPart == 0)
87 if (!QueryPerformanceFrequency(&freq))
91 #elif defined(CRYPTOPP_UNIX_AVAILABLE) 94 return CLOCKS_PER_SEC;
98 #endif // #ifndef CRYPTOPP_IMPORTS 100 TimerWord ThreadUserTimer::GetCurrentTimerValue()
102 #if defined(CRYPTOPP_WIN32_AVAILABLE) 103 static bool getCurrentThreadImplemented =
true;
104 if (getCurrentThreadImplemented)
106 FILETIME now, ignored;
107 if (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now))
109 DWORD lastError = GetLastError();
110 if (lastError == ERROR_CALL_NOT_IMPLEMENTED)
112 getCurrentThreadImplemented =
false;
113 goto GetCurrentThreadNotImplemented;
117 return now.dwLowDateTime + ((TimerWord)now.dwHighDateTime << 32);
119 GetCurrentThreadNotImplemented:
120 return (TimerWord)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
121 #elif defined(CRYPTOPP_UNIX_AVAILABLE) 124 return now.tms_utime;
130 TimerWord ThreadUserTimer::TicksPerSecond()
132 #if defined(CRYPTOPP_WIN32_AVAILABLE) 134 #elif defined(CRYPTOPP_UNIX_AVAILABLE) 135 static const long ticksPerSecond = sysconf(_SC_CLK_TCK);
136 return ticksPerSecond;
138 return CLOCKS_PER_SEC;
Base class for all exceptions thrown by Crypto++.
Utility functions for the Crypto++ library.
Some other error occurred not belong to any of the above categories.
#define COUNTOF(arr)
Counts elements in an array.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Crypto++ library namespace.