-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
timinglib_timestamps_rapid.c
114 lines (81 loc) · 2.47 KB
/
timinglib_timestamps_rapid.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
111
112
113
114
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
Rapidly do `nanos()` calls and load the results into an array, and print it out to see how fast
the time samples could be taken.
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_timestamps_rapid.c timinglib.c -o bin/a && bin/a
# 2. In C++
g++ -Wall -Wextra -Werror -O3 -std=c++17 timinglib_timestamps_rapid.c timinglib.c -o bin/a && 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()`
#define NUM_MEASUREMENTS 10000
// int main(int argc, char *argv[]) // alternative prototype
int main()
{
printf("Rapid timestamps.\n\n");
static uint64_t nanos_array[NUM_MEASUREMENTS];
for (size_t i = 0; i < ARRAY_LEN(nanos_array); i++)
{
nanos_array[i] = nanos();
}
// Now print out the time deltas
uint64_t t_old_ns = nanos_array[0];
uint64_t sum_ns = 0;
uint64_t min_dt_ns = UINT64_MAX;
uint64_t max_dt_ns = 0;
for (size_t i = 0; i < ARRAY_LEN(nanos_array); i++)
{
uint64_t t_new_ns = nanos_array[i];
uint64_t dt_ns = t_new_ns - t_old_ns;
sum_ns += dt_ns;
if (dt_ns > max_dt_ns)
{
max_dt_ns = dt_ns;
}
if (dt_ns > 0 && dt_ns < min_dt_ns)
{
min_dt_ns = dt_ns;
}
t_old_ns = t_new_ns;
printf("%3lu: %6lu ns\n", i, dt_ns);
}
printf("\n");
double avg_dt_ns = (double)sum_ns/ARRAY_LEN(nanos_array);
printf("average time required per `nanos()` call = %8.3f ns\n", avg_dt_ns);
printf("minimum time > 0 for a `nanos()` call = %4lu ns\n", min_dt_ns);
printf("maximum time for a `nanos()` call = %4lu ns\n", max_dt_ns);
return 0;
}
/*
SAMPLE OUTPUT:
In C:
End of the output:
9988: 18 ns
9989: 19 ns
9990: 19 ns
9991: 19 ns
9992: 18 ns
9993: 19 ns
9994: 18 ns
9995: 19 ns
9996: 19 ns
9997: 18 ns
9998: 19 ns
9999: 19 ns
average time required per `nanos()` call = 21.340 ns
minimum time > 0 for a `nanos()` call = 17 ns
maximum time for a `nanos()` call = 7286 ns
OR, in C++:
*/