1 #define _POSIX_C_SOURCE 200809L
10 #define B(t) void t();
16 void error__(const char *n, int l, const char *s, ...) {
17 fprintf(stderr, "use error in tests only\n");
21 static unsigned long long start;
22 static unsigned long long ns;
23 //static unsigned long long bytes;
25 #define SEC 1000000000ULL
26 #define MAXN 1000000000
28 static unsigned long long nsclock() {
33 r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
35 #ifdef _POSIX_MONOTONIC_CLOCK
36 r = clock_gettime(CLOCK_MONOTONIC, &ts);
38 r = clock_gettime(CLOCK_REALTIME, &ts);
42 fprintf(stderr, "bench: clock_gettime failed: %s\n", strerror(errno));
45 return ts.tv_sec*SEC + ts.tv_nsec;
55 ns += nsclock() - start;
66 unsigned long long n = 2*SEC/(ns/N + 1);
76 static void run(const char *name, void (*f)()) {
77 fprintf(stderr, "%s:", name);
78 for (N=1; ; N=nextN()) {
83 // fprintf(stderr, "%10d%12llu\n", N, ns);
84 if (ns > SEC || N >= MAXN)
87 fprintf(stderr, "bench: fatal: N <= 0\n");
92 fprintf(stderr, "%10d%10llu ns/op\n", N, ns/N);
94 fprintf(stderr, "%10d%13.2f ns/op\n", N, (double)ns/N);
98 #define B(t) run(#t, t);