summaryrefslogtreecommitdiff
path: root/ccan/timer/benchmarks/benchmark.c
blob: cb7120a6c1057243db69bd6ce05e20e3b4ba8e6b (plain)
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
#include <ccan/time/time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef FIRST_APPROX
#include "first-approx.c"
#endif
#ifdef SECOND_APPROX
#include "second-approx.c"
#endif
#ifdef NO_APPROX
#include "no-approx.c"
#endif

int main(int argc, char *argv[])
{
	struct timespec start, val, val2, end, diff;
	unsigned int i, j, limit = atoi(argv[1] ?: "100000");
	uint64_t val64;

	val = start = time_now();
	val64 = to_u64(start);
	val2.tv_sec = 0;
	val2.tv_nsec = 1;

	for (j = 0; j < limit; j++) {
		for (i = 0; i < limit; i++) {
			val = time_add(val, val2);
			val64 += to_u64(val2);
		}
	}

	end = time_now();

	printf("val64 says %lu.%09lu\n",
	       from_u64(val64).tv_sec,
	       from_u64(val64).tv_nsec);

	printf("val says %lu.%09lu\n",
	       val.tv_sec,
	       val.tv_nsec);

	if (time_greater(val, from_u64(val64)))
		diff = time_sub(val, from_u64(val64));
	else
		diff = time_sub(from_u64(val64), val);

	printf("Time %lluns, error = %i%%\n",
	       (long long)time_to_nsec(time_sub(end, start)),
	       (int)(100 * time_to_nsec(diff) / time_to_nsec(time_sub(val, start))));
	return 0;
}