31#ifndef TIME_MEASUREMENT_H_
32#define TIME_MEASUREMENT_H_
48#define PATTERN something_not_named_PATTERN
55#define POW10_7 10000000
56#define POW10_9 1000000000
60#define CLOCK_REALTIME 0
61#define CLOCK_MONOTONIC 1
62#define CLOCK_PROCESS_CPUTIME_ID 2
63#define CLOCK_THREAD_CPUTIME_ID 3
68#define DELTA_EPOCH_IN_100NS INT64_C(116444736000000000)
70static int lc_set_errno(
int result)
97int clock_gettime(clockid_t clock_id,
struct timespec *tp)
100 LARGE_INTEGER pf, pc;
107 case CLOCK_REALTIME: {
108 GetSystemTimeAsFileTime(&ct.ft);
109 t = ct.u64 - DELTA_EPOCH_IN_100NS;
110 tp->tv_sec = t / POW10_7;
111 tp->tv_nsec = ((int) (t % POW10_7)) * 100;
116 case CLOCK_MONOTONIC: {
117 if (QueryPerformanceFrequency(&pf) == 0)
return lc_set_errno(EINVAL);
119 if (QueryPerformanceCounter(&pc) == 0)
return lc_set_errno(EINVAL);
121 tp->tv_sec = pc.QuadPart / pf.QuadPart;
123 (int) (((pc.QuadPart % pf.QuadPart) * POW10_9 + (pf.QuadPart >> 1)) /
125 if (tp->tv_nsec >= POW10_9) {
127 tp->tv_nsec -= POW10_9;
133 case CLOCK_PROCESS_CPUTIME_ID: {
135 GetProcessTimes(GetCurrentProcess(), &ct.ft, &et.ft, &kt.ft, &ut.ft))
136 return lc_set_errno(EINVAL);
138 tp->tv_sec = t / POW10_7;
139 tp->tv_nsec = ((int) (t % POW10_7)) * 100;
144 case CLOCK_THREAD_CPUTIME_ID: {
145 if (0 == GetThreadTimes(GetCurrentThread(), &ct.ft, &et.ft, &kt.ft, &ut.ft))
146 return lc_set_errno(EINVAL);
148 tp->tv_sec = t / POW10_7;
149 tp->tv_nsec = ((int) (t % POW10_7)) * 100;
157 return lc_set_errno(EINVAL);
186 assert(!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
198 assert(!clock_gettime(CLOCK_MONOTONIC, &ts));
215 struct timespec &result)
217 struct timespec x = xx, y = yy;
219 if (x.tv_nsec < y.tv_nsec) {
220 int nsec = (y.tv_nsec - x.tv_nsec) / 1000000000L + 1;
221 y.tv_nsec -= 1000000000L * nsec;
224 if (x.tv_nsec - y.tv_nsec > 1000000000L) {
225 int nsec = (x.tv_nsec - y.tv_nsec) / 1000000000L;
226 y.tv_nsec += 1000000000L * nsec;
230 result.tv_sec = x.tv_sec - y.tv_sec;
231 result.tv_nsec = x.tv_nsec - y.tv_nsec;
233 return x.tv_sec < y.tv_sec;
248 return 1E6 * x.tv_sec + x.tv_nsec / 1E3;
260 struct timespec diff;
282 struct timespec diff;
284 return 1000000000L * diff.tv_sec + diff.tv_nsec;
Namespace for T-SIMD.
Definition time_measurement.H:161
int timespec_subtract(const struct timespec &xx, const struct timespec &yy, struct timespec &result)
Subtracts two timespecs.
Definition time_measurement.H:214
double timespec_usec(const struct timespec &x)
Converts a timespec to microseconds. Intended for results of timespec_subtract.
Definition time_measurement.H:246
struct timespec getTimeSpec()
Get the current value of this processes CPU time clock.
Definition time_measurement.H:183
struct timespec getTimeSpecMonotonic()
Get the current value of the system-wide real-time clock.
Definition time_measurement.H:195
long int timeSpecDiffNsec(const struct timespec &x, const struct timespec &y)
Computes the difference between two timespec's in nanoseconds.
Definition time_measurement.H:280
double timeSpecDiffUsec(const struct timespec &x, const struct timespec &y)
Computes the difference between two timespec's in microseconds.
Definition time_measurement.H:258