commit 259302b3686b6b1f3056f8bdea54c7341e15df10 Author: Peter J. Holzer Date: Sat Oct 24 01:29:41 2020 +0200 Benchmark linked list in C diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..bed797a --- /dev/null +++ b/c/Makefile @@ -0,0 +1,2 @@ +CFLAGS = -g -O9 +linked-list: diff --git a/c/linked-list.c b/c/linked-list.c new file mode 100644 index 0000000..2b5e984 --- /dev/null +++ b/c/linked-list.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include + + +typedef struct node nodeT; + +struct node { + uint_fast64_t value; + nodeT *next; +}; + +typedef struct result { + size_t count; + double total_time; + double avg_time; + uint_fast64_t sum; +} resultT; + +nodeT *build(size_t n) { + nodeT *first = malloc(sizeof(nodeT)); + nodeT *last = first; + for (size_t i = 1; i < n; i++) { + nodeT *node = malloc(sizeof(nodeT)); + node->value = i; + last->next = node; + last = node; + } + last->next = first; + return first; +} + +resultT traverse(nodeT *p) { + struct timespec ts0; + size_t n = 0; + uint_fast64_t sum = 0; + clock_gettime(CLOCK_MONOTONIC, &ts0); + for (;;) { + for (uint_fast32_t i = 0; i < 1000000; i++) { + p = p->next; + sum += p->value; + n++; + } + + struct timespec ts1; + clock_gettime(CLOCK_MONOTONIC, &ts1); + double dt = (ts1.tv_sec - ts0.tv_sec) + (ts1.tv_nsec - ts0.tv_nsec) / 1E9; + if (dt >= 1) { + resultT r; + r.count = n; + r.total_time = dt; + r.avg_time = dt / n; + r.sum = sum; + return r; + } + } +} + +int main (void) { + size_t n; + for (n = 2; ; n *= 2) { + nodeT *p = build(n); + resultT r = traverse(p); + printf("%zu %zu %g %g %" PRIuFAST64 "\n", + n, r.count, r.total_time, r.avg_time, r.sum); + } +}