]> git.ozlabs.org Git - ccan/blob - ccan/timer/benchmarks/benchmark.c
timers: implementation of lazily-ordered timers.
[ccan] / ccan / timer / benchmarks / benchmark.c
1 #include <ccan/time/time.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #ifdef FIRST_APPROX
7 #include "first-approx.c"
8 #endif
9 #ifdef SECOND_APPROX
10 #include "second-approx.c"
11 #endif
12 #ifdef NO_APPROX
13 #include "no-approx.c"
14 #endif
15
16 int main(int argc, char *argv[])
17 {
18         struct timespec start, val, val2, end, diff;
19         unsigned int i, j, limit = atoi(argv[1] ?: "100000");
20         uint64_t val64;
21
22         val = start = time_now();
23         val64 = to_u64(start);
24         val2.tv_sec = 0;
25         val2.tv_nsec = 1;
26
27         for (j = 0; j < limit; j++) {
28                 for (i = 0; i < limit; i++) {
29                         val = time_add(val, val2);
30                         val64 += to_u64(val2);
31                 }
32         }
33
34         end = time_now();
35
36         printf("val64 says %lu.%09lu\n",
37                from_u64(val64).tv_sec,
38                from_u64(val64).tv_nsec);
39
40         printf("val says %lu.%09lu\n",
41                val.tv_sec,
42                val.tv_nsec);
43
44         if (time_greater(val, from_u64(val64)))
45                 diff = time_sub(val, from_u64(val64));
46         else
47                 diff = time_sub(from_u64(val64), val);
48
49         printf("Time %lluns, error = %i%%\n",
50                (long long)time_to_nsec(time_sub(end, start)),
51                (int)(100 * time_to_nsec(diff) / time_to_nsec(time_sub(val, start))));
52         return 0;
53 }