From 8a27889d505b07d91ecd03ad1cfeb818b9b440f7 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Wed, 29 Oct 2014 20:32:33 -0500 Subject: Add instrumentation Track the number of comparisons, swaps, and rotations performed in each part of the sorting algorithm. --- counts.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 counts.c (limited to 'counts.c') diff --git a/counts.c b/counts.c new file mode 100644 index 0000000..d1f3910 --- /dev/null +++ b/counts.c @@ -0,0 +1,38 @@ +#include +#include + +#include "counts.h" + +struct counts counts[MAX_COUNTS]; + +static void clear(struct counts *c) +{ + *c = (struct counts) {0}; +} + +void clear_all_counts(void) +{ + for (int i = 0; i < MAX_COUNTS; i++) + clear(counts+i); +} + +void clear_accumulator(void) +{ + clear(counts); +} + +void add_counts(struct counts *target, struct counts *snapshot) +{ + target->compare += counts[CURRENT].compare - snapshot->compare; + target->rotate += counts[CURRENT].rotate - snapshot->rotate; + target->swap += counts[CURRENT].swap - snapshot->swap; +} + +void print_counts(void) +{ + char *labels[] = {"total", "sortnet", "overlap", "merge", "move buffer", "distribute"}; + for (int i = 0; i < MAX_COUNTS; i++) { + const struct counts *c = counts+i; + dprintf(42, "%12s: %10lu cmp, %10lu swap, %10lu rotate\n", labels[i], c->compare, c->swap, c->rotate); + } +} -- cgit v1.2.3