commit ce9a85fe725d5b64306da9cf9e08336c3f3cd347
parent 17114baaffe6ad81a2eb88fa79f3c3e82b82d258
Author: bsandro <email@bsandro.tech>
Date: Mon, 1 Dec 2025 18:53:44 +0200
Measuring cpu time
Diffstat:
3 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
.SUFFIXES:
MAKEFLAGS+=-rR
-CFLAGS=-O2 -std=gnu23 -Werror -Wall -Wextra -ffast-math -march=native -I.
+CFLAGS=-Os -std=gnu23 -Werror -Wall -Wextra -ffast-math -march=native -I.
LDFLAGS=-lc
CC=cc
diff --git a/cputime.h b/cputime.h
@@ -0,0 +1,14 @@
+#include <time.h>
+static volatile unsigned long long now = 0;
+
+void __attribute((constructor)) begin() {
+ struct timespec ts;
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
+ now = ts.tv_sec + ts.tv_nsec;
+}
+
+void __attribute((destructor)) end() {
+ struct timespec ts;
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
+ printf("%lluμs\n", ((ts.tv_sec+ts.tv_nsec)-now)/1000);
+}
diff --git a/day01.c b/day01.c
@@ -1,5 +1,5 @@
#include <stdio.h>
-
+#include "cputime.h"
#define BUF_SIZE 32
int main(void) {