summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-04-25 12:52:55 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-04-25 12:52:55 +0330
commit833fbb09640e46f7e8968a1a724baf25f2a219fc (patch)
treedc2b3677cad411b11f8bdf1dfcd50e167cc28396
parent0f50a82f0694bcf2902a772de63cc2b9a007c2f7 (diff)
move time to utils
-rw-r--r--Makefile1
-rw-r--r--src/main.c43
-rw-r--r--src/utils/time.c32
-rw-r--r--src/utils/time.h7
4 files changed, 48 insertions, 35 deletions
diff --git a/Makefile b/Makefile
index 76a2d31..d9f18bf 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ NC := \033[0m
INC_DIRS := $(SRC_DIR)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
+# OP_FLAG := -Ofast
OP_FLAG := -O3
# OP_FLAG := -Oz
# OP_FLAG := -g
diff --git a/src/main.c b/src/main.c
index 323b04b..273f4ec 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,36 +3,9 @@
#include "utils/file.h"
#include "utils/log.h"
#include <stdio.h>
-#include <time.h>
-
-// #define PRINT_COMPILE_TREE
-// #define PRINT_STATISTICS
#ifdef PRINT_STATISTICS
-static struct timespec diff(struct timespec end, struct timespec start) {
- struct timespec temp;
- if ((end.tv_nsec - start.tv_nsec) < 0) {
- temp.tv_sec = end.tv_sec - start.tv_sec - 1;
- temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
- } else {
- temp.tv_sec = end.tv_sec - start.tv_sec;
- temp.tv_nsec = end.tv_nsec - start.tv_nsec;
- }
- return temp;
-}
-
-static struct timespec add(struct timespec left, struct timespec right) {
- struct timespec result;
- result.tv_nsec = left.tv_nsec + right.tv_nsec;
- result.tv_sec = (left.tv_sec + right.tv_sec) + result.tv_nsec / 1000000000;
- result.tv_nsec %= 1000000000;
- return result;
-}
-
-static void printTime(struct timespec time) {
- printf("%02ld:%02ld.%06ldus", time.tv_sec / 60, time.tv_sec % 60,
- time.tv_nsec / 1000);
-}
+#include "utils/time.h"
#endif
static int run(const char *filePath) {
@@ -49,8 +22,8 @@ static int run(const char *filePath) {
}
#ifdef PRINT_STATISTICS
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
- astTime = diff(end, start);
- totalTime = add(totalTime, astTime);
+ astTime = time_diff(end, start);
+ totalTime = time_add(totalTime, astTime);
#endif
#ifdef PRINT_COMPILE_TREE
for (size_t i = 0; i < astTrees.size; ++i) {
@@ -70,17 +43,17 @@ static int run(const char *filePath) {
astTreeRootsDestroy(astTrees);
#ifdef PRINT_STATISTICS
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
- runTime = diff(end, start);
- totalTime = add(totalTime, runTime);
+ runTime = time_diff(end, start);
+ totalTime = time_add(totalTime, runTime);
#endif
#ifdef PRINT_STATISTICS
printf("\nastTime: ");
- printTime(astTime);
+ time_print(astTime);
printf("\nrunTime: ");
- printTime(runTime);
+ time_print(runTime);
printf("\ntotal: ");
- printTime(totalTime);
+ time_print(totalTime);
printf("\n");
#endif
diff --git a/src/utils/time.c b/src/utils/time.c
new file mode 100644
index 0000000..979a2c8
--- /dev/null
+++ b/src/utils/time.c
@@ -0,0 +1,32 @@
+#include "time.h"
+
+#include <stdio.h>
+
+#ifdef PRINT_STATISTICS
+struct timespec time_diff(struct timespec end, struct timespec start) {
+ struct timespec temp;
+ if ((end.tv_nsec - start.tv_nsec) < 0) {
+ temp.tv_sec = end.tv_sec - start.tv_sec - 1;
+ temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
+ } else {
+ temp.tv_sec = end.tv_sec - start.tv_sec;
+ temp.tv_nsec = end.tv_nsec - start.tv_nsec;
+ }
+ return temp;
+}
+
+struct timespec time_add(struct timespec left, struct timespec right) {
+ struct timespec result;
+ result.tv_nsec = left.tv_nsec + right.tv_nsec;
+ result.tv_sec = (left.tv_sec + right.tv_sec) + result.tv_nsec / 1000000000;
+ result.tv_nsec %= 1000000000;
+ return result;
+}
+
+void time_print(struct timespec time) {
+ printf("%02ld:%02ld.%06ldus", time.tv_sec / 60, time.tv_sec % 60,
+ time.tv_nsec / 1000);
+}
+#endif
+
+
diff --git a/src/utils/time.h b/src/utils/time.h
new file mode 100644
index 0000000..6d5f2f6
--- /dev/null
+++ b/src/utils/time.h
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <time.h>
+
+struct timespec time_diff(struct timespec end, struct timespec start);
+struct timespec time_add(struct timespec left, struct timespec right);
+void time_print(struct timespec time);