Skip to content

Instantly share code, notes, and snippets.

@zoq
Created June 7, 2013 20:23
Show Gist options
  • Save zoq/5732141 to your computer and use it in GitHub Desktop.
Save zoq/5732141 to your computer and use it in GitHub Desktop.
timer
#if defined(__unix__) || defined(__unix)
#include <time.h> // clock_gettime()
#include <sys/time.h> // timeval, gettimeofday()
#include <unistd.h> // flags like _POSIX_VERSION
#elif defined(__MACH__) && defined(__APPLE__)
#include <mach/mach_time.h> // mach_timebase_info,
// mach_absolute_time()
#elif defined(_WIN32)
#include <windows.h> //GetSystemTimeAsFileTime(),
// QueryPerformanceFrequency(),
// QueryPerformanceCounter()
// uint64_t isn't defined on every windows.
#if !defined(HAVE_UINT64_T)
#if SIZEOF_UNSIGNED_LONG == 8
typedef unsigned long uint64_t;
#else
typedef unsigned long long uint64_t;
#endif // SIZEOF_UNSIGNED_LONG
#endif // HAVE_UINT64_T
//gettimeofday has no equivalent will need to write extra code for that.
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif // _MSC_VER, _MSC_EXTENSIONS
#else
#error "unknown OS"
#endif
#include <iostream>
double GetTime()
{
#if defined(__MACH__) && defined(__APPLE__)
static double convert = 0.0;
// If this is the first time we've run, get the timebase.
// We use convert == 0 to indicate that sTimebaseInfo is
// uninitialised.
if (convert == 0.0) {
static mach_timebase_info_data_t sTimebaseInfo;
(void)mach_timebase_info(&sTimebaseInfo);
convert = (double)sTimebaseInfo.numer /
(double)sTimebaseInfo.denom /
1000000000.0;
}
// Hope that the multiplication doesn't overflow.
return (double)mach_absolute_time() * convert;
#elif defined(_POSIX_VERSION)
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
struct timespec ts;
// Get the right clock_id.
#if defined(CLOCK_MONOTONIC_PRECISE)
static const clockid_t id = CLOCK_MONOTONIC_PRECISE;
#elif defined(CLOCK_MONOTONIC_RAW)
static const clockid_t id = CLOCK_MONOTONIC_RAW;
#elif defined(CLOCK_MONOTONIC)
static const clockid_t id = CLOCK_MONOTONIC;
#elif defined(CLOCK_REALTIME)
static const clockid_t id = CLOCK_REALTIME;
#else
static const clockid_t id = ((clockid_t)-1);
#endif // CLOCK
// Returns the current value tp for the specified clock_id.
if (clock_gettime(id, &ts) != -1 && id != ((clockid_t)-1))
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
// Fallback for clock_gettime function.
timeval b;
gettimeofday(&b, NULL);
return (double)b.tv_sec + (double)b.tv_usec / 1000000.0;
#endif // _POSIX_TIMERS
#elif defined(_WIN32)
static double frequency = 0.0;
// If this is the first time we've run, get the frequency.
// We use frequency == 0 to indicate that
// QueryPerformanceFrequency is uninitialised.
if (frequency == 0.0)
{
LARGE_INTEGER pF;
if (!QueryPerformanceFrequency(&pF))
{
// Fallback for QueryPerformanceCounter function.
FILETIME ftime;
uint64_t ptime = 0;
// Acquire the file time.
GetSystemTimeAsFileTime(&ftime);
// Now convert FILETIME.
ptime |= ftime.dwHighDateTime;
ptime = ptime << 32;
ptime |= ftime.dwLowDateTime;
ptime /= 10;
ptime -= DELTA_EPOCH_IN_MICROSECS;
return (double)(ptime / 1000000UL)
+ (double)(ptime % 1000000UL) / 1000000.0;
}
else
frequency = 1.0 / double(pF.QuadPart);
}
// Get the current performance-counter value.
LARGE_INTEGER pC;
QueryPerformanceCounter(&pC);
return double(pC.QuadPart)*frequency;
#endif
// Timer initialization failed.
return -1;
}
int main()
{
double start, end;
start = GetTime();
sleep(2);
end = GetTime();
std::cout << "time: " << (end - start) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment