-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.c
63 lines (58 loc) · 1.51 KB
/
timing.c
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
// Timing measurement utilities implementation.
#include "timing.h"
#include <stddef.h>
void
timing_set(
struct timing * t)
{
#ifdef USE_CLOCK
t->clock = clock();
#else
gettimeofday(&t->tv, NULL);
#endif
}
double
timing_get(
struct timing * t)
{
#ifdef USE_CLOCK
// The clock_t type is an "arithmetic type", which could be
// integral, double, long double, or others.
//
// Add 0.0 to make it a double or long double, then divide (in
// double or long double), then convert to double for our purposes.
return (double) ((t->clock + 0.0) / CLOCKS_PER_SEC);
#else
return (double) t->tv.tv_sec + ((double) t->tv.tv_usec) / 1000000.0;
#endif
}
double
timing_now()
{
#ifdef USE_CLOCK
return (double) ((clock() + 0.0) / CLOCKS_PER_SEC);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (double) tv.tv_sec + ((double) tv.tv_usec) / 1000000.0;
#endif
}
double
timing_delta(
struct timing * t1,
struct timing * t2)
{
#ifdef USE_CLOCK
// The clock_t type is an "arithmetic type", which could be
// integral, double, long double, or others.
//
// Subtract first, resulting in another clock_t, then add 0.0 to
// make it a double or long double, then divide (in double or long
// double), then convert to double for our purposes.
return (double) (((t2->clock - t1->clock) + 0.0) / CLOCKS_PER_SEC);
#else
double const d2 = (double) t2->tv.tv_sec + ((double) t2->tv.tv_usec) / 1000000.0;
double const d1 = (double) t1->tv.tv_sec + ((double) t1->tv.tv_usec) / 1000000.0;
return d2 - d1;
#endif
}