diff options
Diffstat (limited to 'tools/timerun.h')
| -rw-r--r-- | tools/timerun.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tools/timerun.h b/tools/timerun.h new file mode 100644 index 0000000..dabfde0 --- /dev/null +++ b/tools/timerun.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <inttypes.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | double | ||
| 8 | timerun(void (*run)(void), char *name) | ||
| 9 | { | ||
| 10 | struct timespec start, end; | ||
| 11 | double tdiff, tdsec, tdnano; | ||
| 12 | |||
| 13 | printf("\n"); | ||
| 14 | fflush(stdout); | ||
| 15 | |||
| 16 | if (run == NULL) { | ||
| 17 | printf("> %s: nothing to run!\n", name); | ||
| 18 | fflush(stdout); | ||
| 19 | return -1.0; | ||
| 20 | } | ||
| 21 | |||
| 22 | printf("Running tool: %s\n", name); | ||
| 23 | printf("==========\n"); | ||
| 24 | fflush(stdout); | ||
| 25 | |||
| 26 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 27 | run(); | ||
| 28 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 29 | |||
| 30 | tdsec = end.tv_sec - start.tv_sec; | ||
| 31 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 32 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 33 | |||
| 34 | printf("==========\n"); | ||
| 35 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 36 | fflush(stdout); | ||
| 37 | |||
| 38 | return tdiff; | ||
| 39 | } | ||
