#include #include #include #include #include "testcases.h" #include "sorters.h" #include "common.h" #include "counts.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++) { clear_all_counts(); 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, counts[CURRENT].compare, MS_WIDTH, timediff_ms(&start, &stop)); print_counts(); assert_sorted((void*)buffer, size, sizeof(int), t->cmp); } printf(" %*zu\n", SIZE_WIDTH, size); } puts(""); } return 0; }