From 85e7981251183b1da785a78bf9a81ddfe0a156eb Mon Sep 17 00:00:00 2001
From: Orestis <orestis.malaspinas@pm.me>
Date: Wed, 27 Oct 2021 11:00:54 +0200
Subject: [PATCH] updated complexity example

---
 complexite.md => slides/complexite.md |  0
 source_codes/complexity/.gitignore    |  5 +++++
 source_codes/complexity/Makefile      | 20 ++++++++++++++++++++
 source_codes/complexity/sum.c         |  7 ++++++-
 4 files changed, 31 insertions(+), 1 deletion(-)
 rename complexite.md => slides/complexite.md (100%)

diff --git a/complexite.md b/slides/complexite.md
similarity index 100%
rename from complexite.md
rename to slides/complexite.md
diff --git a/source_codes/complexity/.gitignore b/source_codes/complexity/.gitignore
index 9229287..7878686 100644
--- a/source_codes/complexity/.gitignore
+++ b/source_codes/complexity/.gitignore
@@ -1 +1,6 @@
 sum
+sum_one
+sum_one_opt
+sum_thousand
+sum_thousand_opt
+
diff --git a/source_codes/complexity/Makefile b/source_codes/complexity/Makefile
index d7822ac..7159661 100644
--- a/source_codes/complexity/Makefile
+++ b/source_codes/complexity/Makefile
@@ -11,6 +11,26 @@ $(EXECS): %: %.c
 	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
 	@echo $@ >> .gitignore
 
+bench: sum_one sum_one_opt sum_thousand sum_thousand_opt
+	@echo "RUN ONCE O0" && ./sum_one
+	@echo "RUN ONCE O3" && ./sum_one_opt
+	@echo "RUN THOUSAND TIMES O0" && ./sum_thousand
+	@echo "RUN THOUSAND TIMES O3" && ./sum_thousand_opt
+
+
+sum_one: sum.c
+	$(CC) $(CFLAGS) -DSIZE=1000000 -DNUM_TIMES=1 -o $@ $< $(LDFLAGS) 
+
+sum_one_opt: sum.c
+	$(CC) $(CFLAGS) -O3 -DSIZE=1000000 -DNUM_TIMES=1 -o $@ $< $(LDFLAGS) 
+
+sum_thousand: sum.c
+	$(CC) $(CFLAGS) -DSIZE=1000000 -DNUM_TIMES=1000 -o $@ $< $(LDFLAGS) 
+
+sum_thousand_opt: sum.c
+	$(CC) $(CFLAGS) -O3 -DSIZE=1000000 -DNUM_TIMES=1000 -o $@ $< $(LDFLAGS) 
+
+
 .PHONY: clean all
 
 clean:
diff --git a/source_codes/complexity/sum.c b/source_codes/complexity/sum.c
index 4451354..4a9780a 100644
--- a/source_codes/complexity/sum.c
+++ b/source_codes/complexity/sum.c
@@ -2,8 +2,13 @@
 #include <stdlib.h>
 #include <time.h>
 
+#ifndef SIZE
 #define SIZE 1000000
+#endif
+
+#ifndef NUM_TIMES
 #define NUM_TIMES 10
+#endif
 
 void init(int n, double tab[]) {
     for (int i = 0; i < n; ++i) {
@@ -30,7 +35,7 @@ int main() {
         s += sum(SIZE, tab);
     }
     clock_gettime(CLOCK_MONOTONIC, &tend);
-    printf("the computation of %f took about %.5f seconds\n", s,
+    printf("the computation took about %.5f seconds\n",
         (((double)tend.tv_sec + 1e-9 * tend.tv_nsec) -
             ((double)tstart.tv_sec + 1e-9 * tstart.tv_nsec)) /
             NUM_TIMES);
-- 
GitLab