-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgettime.cpp
58 lines (47 loc) · 1.43 KB
/
gettime.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
#include "faasm/faasm.h"
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
// Time with direct syscall
timespec tsA{};
clock_gettime(CLOCK_REALTIME, &tsA);
// Time with clock
clock_t clockStart = clock();
usleep(10);
printf("Doing something...\n");
// End with direct syscall
timespec tsB{};
clock_gettime(CLOCK_REALTIME, &tsB);
// End with clock
clock_t clockEnd = clock();
// Work out total syscall diff
long nano = 1000000000;
long totalTimeA = (tsA.tv_sec * nano) + tsA.tv_nsec;
long totalTimeB = (tsB.tv_sec * nano) + tsB.tv_nsec;
long diff = totalTimeB - totalTimeA;
// Work out clock diff
if (CLOCKS_PER_SEC != 1000000) {
printf("CLOCKS_PER_SEC not as expected (%li)", CLOCKS_PER_SEC);
}
double clockDiff = (double)(clockEnd - clockStart) / CLOCKS_PER_SEC;
// Checks
if (diff <= 0) {
printf("Elapsed time from syscalls not greater later (diff = %lins)\n",
diff);
return 1;
} else {
printf("Elapsed time from syscalls greater later (diff = %lins)\n",
diff);
}
if (clockDiff <= 0) {
printf("Elapsed time from clock not greater later (diff = %fs)\n",
clockDiff);
return 1;
} else {
printf("Elapsed time from clock greater later (diff = %fs)\n",
clockDiff);
}
return 0;
}