diff options
Diffstat (limited to 'benchtests/bench-skeleton.c')
-rw-r--r-- | benchtests/bench-skeleton.c | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/benchtests/bench-skeleton.c b/benchtests/bench-skeleton.c index 13f986d817..bbd151b0ec 100644 --- a/benchtests/bench-skeleton.c +++ b/benchtests/bench-skeleton.c @@ -22,13 +22,18 @@ #include <time.h> #include <inttypes.h> +#define TIMESPEC_AFTER(a, b) \ + (((a).tv_sec == (b).tv_sec) ? \ + ((a).tv_nsec > (b).tv_nsec) : \ + ((a).tv_sec > (b).tv_sec)) int main (int argc, char **argv) { - unsigned long i, j, k; + unsigned long i, k; uint64_t total = 0, max = 0, min = 0x7fffffffffffffff; - struct timespec start, end; + struct timespec start, end, runtime; + memset (&runtime, 0, sizeof (runtime)); memset (&start, 0, sizeof (start)); memset (&end, 0, sizeof (end)); @@ -39,11 +44,15 @@ main (int argc, char **argv) Measurements close to the minimum clock resolution won't make much sense, but it's better than having nothing at all. */ unsigned long iters = 1000 * start.tv_nsec; - unsigned long total_iters = ITER / iters; - for (i = 0; i < NUM_SAMPLES; i++) + /* Run for approxmately DURATION seconds. */ + clock_gettime (CLOCK_MONOTONIC_RAW, &runtime); + runtime.tv_sec += DURATION; + + double d_total_i = 0; + while (1) { - for (j = 0; j < total_iters; j ++) + for (i = 0; i < NUM_SAMPLES; i++) { clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &start); for (k = 0; k < iters; k++) @@ -61,12 +70,25 @@ main (int argc, char **argv) min = cur; total += cur; + + d_total_i += iters; } + + struct timespec curtime; + + memset (&curtime, 0, sizeof (curtime)); + clock_gettime (CLOCK_MONOTONIC_RAW, &curtime); + if (TIMESPEC_AFTER (curtime, runtime)) + goto done; } - double d_total_s = total * 1e-9; - double d_iters = iters; - double d_total_i = (double)ITER * NUM_SAMPLES; + double d_total_s; + double d_iters; + + done: + d_total_s = total * 1e-9; + d_iters = iters; + printf (FUNCNAME ": ITERS:%g: TOTAL:%gs, MAX:%gns, MIN:%gns, %g iter/s\n", d_total_i, d_total_s, max / d_iters, min / d_iters, d_total_i / d_total_s); |