#include #include #include #include #include "testcases.h" #include "sorters.h" #define CMP_WIDTH 12 #define MS_WIDTH 6 #define SORT_WIDTH 16 #define TEST_WIDTH (CMP_WIDTH + MS_WIDTH + 1) #define SIZE_WIDTH 10 static inline unsigned long timediff_ms(struct timespec *start, struct timespec *stop) { return 1000 * (stop->tv_sec - start->tv_sec) + (stop->tv_nsec - start->tv_nsec) / 1000000; } int main() { struct timespec start, stop; printf("%-*s ", SORT_WIDTH, ""); for (const struct testcase *t = testcases; t->name; t++) printf("%*s ", TEST_WIDTH, t->name); printf(" %*s\n%-*s ", SIZE_WIDTH, "elements", SORT_WIDTH, ""); for (const struct testcase *t = testcases; t->name; t++) printf("%*s %*s ", CMP_WIDTH, "compares", MS_WIDTH, "ms"); puts(""); for (const struct sorter *s = sorters; s->name; s++) { for (size_t size = MIN_SIZE; size <= MAX_SIZE; size *= 10) { printf("%-*s ", SORT_WIDTH, size == MIN_SIZE ? s->name : ""); for (const struct testcase *t = testcases; t->name; t++) { comparisons = 0; t->init(size); if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start)) abort(); s->func(buffer, size, sizeof(int), t->cmp); if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop)) abort(); printf("%*lu %*lu ", CMP_WIDTH, comparisons, MS_WIDTH, timediff_ms(&start, &stop)); assert_sorted(size, t->cmp); } printf(" %*zu\n", SIZE_WIDTH, size); } puts(""); } return 0; }