-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
timinglib_get_resolution.c
110 lines (71 loc) · 2.62 KB
/
timinglib_get_resolution.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
Test the new get_resolution() function to estimate the resolution of timestamps.
STATUS: done!
To compile and run (assuming you've already `cd`ed into this dir):
```bash
# 1. In C:
gcc -Wall -Wextra -Werror -O3 -std=c17 timinglib_get_resolution.c timinglib.c -o bin/a && time bin/a
# 2. In C++
g++ -Wall -Wextra -Werror -O3 -std=c++17 timinglib_get_resolution.c timinglib.c -o bin/a && time bin/a
```
References:
1.
*/
// Local includes
#include "timinglib.h"
// Linux includes
// C includes
#include <stdbool.h> // For `true` (`1`) and `false` (`0`) macros in C
#include <stdint.h> // For `uint8_t`, `int8_t`, etc.
#include <stdio.h> // For `printf()`
// int main(int argc, char *argv[]) // alternative prototype
int main()
{
printf("Hello World.\n\n");
uint64_t estimated_resolution_ns = get_estimated_resolution();
printf("estimated_resolution_ns = %lu ns\n", estimated_resolution_ns);
uint64_t specified_resolution_ns = get_specified_resolution();
printf("specified_resolution_ns = %lu ns\n", specified_resolution_ns);
return 0;
}
/*
SAMPLE OUTPUT:
In both C and C++, typical resolutions for me are ~14~27 ns.
For `clock_gettime()`, this is only when using `CLOCK_MONOTONIC`:
`clock_gettime(CLOCK_MONOTONIC, &ts)`
In C:
eRCaGuy_hello_world/c$ g++ -Wall -Wextra -Werror -O3 -std=c++17 timinglib_get_resolution.c timinglib.c -o bin/a && time bin/a
Hello World.
estimated_resolution_ns = 15 ns
specified_resolution_ns = 1 ns
real 0m0.001s
user 0m0.001s
sys 0m0.000s
OR, in C++:
eRCaGuy_hello_world/c$ g++ -Wall -Wextra -Werror -O3 -std=c++17 timinglib_get_resolution.c timinglib.c -o bin/a && time bin/a
Hello World.
estimated_resolution_ns = 15 ns
specified_resolution_ns = 1 ns
real 0m0.001s
user 0m0.001s
sys 0m0.000s
**********************************************
For `clock_gettime(CLOCK_MONOTONIC_RAW, &ts)`:
**********************************************
Here are a few runs:
eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 timinglib_get_resolution.c timinglib.c -o bin/a && time bin/a
Hello World.
estimated_resolution_ns = 97 ns
specified_resolution_ns = 1 ns
real 0m0.001s
user 0m0.001s
sys 0m0.000s
eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 timinglib_get_resolution.c timinglib.c -o bin/a && time bin/a
Hello World.
estimated_resolution_ns = 92 ns
specified_resolution_ns = 1 ns
real 0m0.001s
user 0m0.001s
sys 0m0.000s
*/