-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_timer.cpp
97 lines (77 loc) · 1.8 KB
/
my_timer.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "my_timer.h"
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef HighResTimer
inline uint64_t __invoke_rdtsc(void)
{
#if defined(__i386__)
uint64_t x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
#elif defined(__x86_64__)
uint32_t lo, hi;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return (uint64_t)lo | (((uint64_t)hi) << 32);
#else
return 0;
#endif
}
#if 0
uint64_t __invoke_rdtscp(void)
{
uint32_t lo, hi;
__asm__ volatile ("rdtscp"
: /* outputs */ "=a" (lo), "=d" (hi)
: /* no inputs */
: /* clobbers */ "%rcx");
return (uint64_t)lo | (((uint64_t)hi) << 32);
}
#endif
//inline
uint64_t getClockTicks(void)
{
return __invoke_rdtsc();
}
myTimer_t getTimeStamp()
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t;
}
double getElapsedTime(myTimer_t t0, myTimer_t t1)
{
return double(t1.tv_sec - t0.tv_sec) + 1e-9 * double(t1.tv_nsec - t0.tv_nsec);
}
#else
/* "Accurate" wall-clock timing function. */
myTimer_t getTimeStamp (void)
{
struct timeval t;
gettimeofday (&t, NULL);
return t;
}
double getElapsedTime(myTimer_t t0, myTimer_t t1)
{
return double(t1.tv_sec - t0.tv_sec) + 1.0e-6 * double(t1.tv_usec - t0.tv_usec);
}
uint64_t getClockTicks(void) { return 0; }
#endif
double getTicksPerSecond(void)
{
uint64_t tick_start = getClockTicks();
uint64_t sum = 0;
for (int i = 0; i < 100000; ++i) sum += rand();
uint64_t tick_stop = getClockTicks();
if (tick_stop - tick_start <= 0)
return 0;
myTimer_t time_start = getTimeStamp();
for (int i = 0; i < 100000; ++i) sum += rand();
myTimer_t time_stop = getTimeStamp();
double time_diff = getElapsedTime(time_start, time_stop);
return double(tick_stop - tick_start) / time_diff;
}
#ifdef __cplusplus
} // extern "C"
#endif