From 448afb5bb09f4858f7f95ff873931674ad47defa Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Thu, 12 Oct 2017 20:12:04 +0100
Subject: [PATCH] Provides average stats on completion.

---
 cube-test.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/cube-test.c b/cube-test.c
index b394d0f..71a6c2e 100644
--- a/cube-test.c
+++ b/cube-test.c
@@ -51,11 +51,25 @@ end_timer(timer_t *timer) {
   return delta;
 }
 
+char *
+humanize_usec(long int usec) {
+  static char buf[40];
+  if (usec < 1000) {
+    sprintf(buf, "%ldus", usec);
+  } else if (usec < 1000000) {
+    sprintf(buf, "%.1fms", usec / 1000.0);
+  } else {
+    sprintf(buf, "%.2fs", usec / 1000000.0);
+  }
+
+  return buf;
+}
+
 int
 main(int argc, char *argv[]) {
-  int guess, number;
+  int guess, number, iterations = 0, correct = 0;
   timer_t timer;
-  long int delta;
+  long int delta, delta_total = 0;
 
   srand(time(NULL));
 
@@ -66,19 +80,19 @@ main(int argc, char *argv[]) {
     guess = read_number();
     delta = end_timer(&timer);
     if (guess == 0) {
+      printf("Got %d correct out of %d.\n", correct, iterations);
+      printf("Average time to answer was %s\n",
+          humanize_usec(delta_total / iterations));
       exit(0);
     }
+    iterations++;
+    delta_total += delta;
     if (guess == number) {
+      correct++;
       printf("Correct.\n");
     } else {
       printf("Not correct. Number was %d.\n", number);
     }
-    if (delta < 1000) {
-      printf("Took %ldus\n", delta);
-    } else if (delta < 1000000) {
-      printf("Took %.1fms\n", delta / 1000.0);
-    } else {
-      printf("Took %.2fs\n", delta / 1000000.0);
-    }
+    printf("Took %s\n", humanize_usec(delta));
   }
 }
-- 
GitLab