From 97248c9681a5b08ff4ee8d8b55e4d94d1f41b4d0 Mon Sep 17 00:00:00 2001
From: Philippe Pittoli <karchnu@karchnu.fr>
Date: Sun, 2 Feb 2025 20:03:44 +0100
Subject: [PATCH] Add a time measuring lib to copy/paste.

---
 c/time-measuring.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 c/time-measuring.c

diff --git a/c/time-measuring.c b/c/time-measuring.c
new file mode 100644
index 0000000..60ca334
--- /dev/null
+++ b/c/time-measuring.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+//#include <sys/time.h>
+#include <time.h>
+#include <stdlib.h>
+
+#define BILLION  1000000000L
+#define MILLION  1000000L
+#define THOUSAND 1000L
+
+void get_time_ts(struct timespec *t0) {
+	if (clock_gettime(CLOCK_REALTIME, t0) == -1) {
+		perror("clock_gettime");
+		exit(1);
+	}
+}
+
+long long duration_ns(struct timespec t0, struct timespec t1) {
+	return ((t1.tv_sec - t0.tv_sec)*BILLION) + (t1.tv_nsec - t0.tv_nsec);
+}
+
+void print_time_ns(long long time) {
+	printf("%llds %lldms %lldµ %lldns\n"
+		, time/BILLION
+		, (time/MILLION)%THOUSAND
+		, (time/THOUSAND)%THOUSAND
+		, time%THOUSAND
+	);
+}
+
+int main (int argc, char** argv) {
+	struct timespec t0, t1;
+
+	get_time_ts(&t0);
+
+	int i = 0;
+	while (i < 10000) { i++; }
+
+	get_time_ts(&t1);
+
+	long long time = duration_ns(t0, t1);
+
+	print_time_ns(time);
+
+	return 0;
+}
+
+/*  // For limited precision: timeval structure + gettimeofday function.
+	struct timeval t0, t1;
+
+	gettimeofday (&t0, NULL);
+	int i = 0;
+	while (i < 10000) { i++; }
+
+	gettimeofday (&t1, NULL);
+
+	printf("%ld ms\n", (t1.tv_sec - t0.tv_sec)*BILLION + (t1.tv_usec - t0.tv_usec));
+*/