Skip to content
Snippets Groups Projects
Verified Commit 85e79812 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

updated complexity example

parent 377278e8
No related branches found
No related tags found
No related merge requests found
File moved
sum sum
sum_one
sum_one_opt
sum_thousand
sum_thousand_opt
...@@ -11,6 +11,26 @@ $(EXECS): %: %.c ...@@ -11,6 +11,26 @@ $(EXECS): %: %.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
@echo $@ >> .gitignore @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 .PHONY: clean all
clean: clean:
......
...@@ -2,8 +2,13 @@ ...@@ -2,8 +2,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#ifndef SIZE
#define SIZE 1000000 #define SIZE 1000000
#endif
#ifndef NUM_TIMES
#define NUM_TIMES 10 #define NUM_TIMES 10
#endif
void init(int n, double tab[]) { void init(int n, double tab[]) {
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
...@@ -30,7 +35,7 @@ int main() { ...@@ -30,7 +35,7 @@ int main() {
s += sum(SIZE, tab); s += sum(SIZE, tab);
} }
clock_gettime(CLOCK_MONOTONIC, &tend); 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)tend.tv_sec + 1e-9 * tend.tv_nsec) -
((double)tstart.tv_sec + 1e-9 * tstart.tv_nsec)) / ((double)tstart.tv_sec + 1e-9 * tstart.tv_nsec)) /
NUM_TIMES); NUM_TIMES);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment