1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#ifndef COMMON_TIMER_HPP
#define COMMON_TIMER_HPP
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include <sstream>
#include <iostream>
using namespace std;
class Timer {
private:
#ifdef WIN32
LARGE_INTEGER ticksPerSecond, tick1, tick2;
DWORD temps;
#else
struct timeval sTime;
#endif
public:
static unsigned long getTickCount()
{
#ifdef WIN32
// return number of ms since last reboot
return GetTickCount();
#else
// return number of ms since 1970/1/1
struct timeval cTime;
gettimeofday(&cTime, 0);
return (cTime.tv_sec * 1000) + (cTime.tv_usec / 1000);
#endif
}
void initTimer()
{
#ifdef WIN32
temps = GetTickCount();
QueryPerformanceFrequency(&ticksPerSecond);
QueryPerformanceCounter(&tick1);
#else
gettimeofday(&sTime, 0);
#endif
}
unsigned long getMilliSec()
{
#ifdef WIN32
return GetTickCount() - temps;
#else
struct timeval cTime;
gettimeofday(&cTime, 0);
return ((cTime.tv_sec - sTime.tv_sec) * 1000)
+ ((cTime.tv_usec - sTime.tv_usec) / 1000);
#endif
}
unsigned long getMicroSec()
{
#ifdef WIN32
QueryPerformanceCounter(&tick2);
return ((tick2.QuadPart - tick1.QuadPart) * 1000000) / ticksPerSecond.QuadPart;
#else
struct timeval cTime;
gettimeofday(&cTime, 0);
return ((cTime.tv_sec - sTime.tv_sec) * 1000000)
+ ((cTime.tv_usec - sTime.tv_usec));
#endif
}
};
#endif |