summaryrefslogtreecommitdiff
path: root/bench.c
diff options
context:
space:
mode:
Diffstat (limited to 'bench.c')
-rw-r--r--bench.c36
1 files changed, 8 insertions, 28 deletions
diff --git a/bench.c b/bench.c
index abcaca9..e0629b2 100644
--- a/bench.c
+++ b/bench.c
@@ -3,33 +3,13 @@
#include <stdlib.h>
#include <time.h>
-#include "generators.h"
+#include "testcases.h"
#include "sorters.h"
-#define MIN_SIZE 10000
-#define MAX_SIZE 10000000
-
-static int buffer[MAX_SIZE];
-static unsigned long comparisons;
-
-static int compare(const void *a, const void *b)
-{
- const int *aa = a;
- const int *bb = b;
-
- comparisons++;
- if (*aa < *bb)
- return -1;
- else if (*aa > *bb)
- return 1;
- else
- return 0;
-}
-
#define CMP_WIDTH 12
#define MS_WIDTH 6
#define SORT_WIDTH 16
-#define GEN_WIDTH (CMP_WIDTH + MS_WIDTH + 1)
+#define TEST_WIDTH (CMP_WIDTH + MS_WIDTH + 1)
#define SIZE_WIDTH 10
static inline unsigned long timediff_ms(struct timespec *start, struct timespec *stop)
@@ -42,22 +22,22 @@ int main()
struct timespec start, stop;
printf("%-*s ", SORT_WIDTH, "");
- for (const struct generator *g = generators; g->name; g++)
- printf("%*s ", GEN_WIDTH, g->name);
+ 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 generator *g = generators; g->name; g++)
+ 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 generator *g = generators; g->name; g++) {
+ for (const struct testcase *t = testcases; t->name; t++) {
comparisons = 0;
- g->func(buffer, size);
+ t->init(buffer, size);
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start)) abort();
- s->func(buffer, size, sizeof(int), compare);
+ 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));