diff --git a/code_optimization.pptx b/code_optimization.pptx index ff993ef..18fdc53 100644 Binary files a/code_optimization.pptx and b/code_optimization.pptx differ diff --git a/source-code/Branching/Makefile b/source-code/Branching/Makefile index b1859a9..2a1b8bd 100644 --- a/source-code/Branching/Makefile +++ b/source-code/Branching/Makefile @@ -1,9 +1,10 @@ CFLAGS = -O2 -g -Wall -Wextra +LDLIBS = -lm all: branch.exe %.exe: %.c - $(CC) $(CFLAGS) -o $@ $< + $(CC) $(CFLAGS) -o $@ $< $(LDLIBS) clean: $(RM) *.exe *.o diff --git a/source-code/Branching/branch.c b/source-code/Branching/branch.c index dd91bf5..cbf6390 100644 --- a/source-code/Branching/branch.c +++ b/source-code/Branching/branch.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -13,11 +14,12 @@ double *init(int n) { return a; } -double *init_cont(int n, int count) { +double *init_cont(int n, double fraction) { int i; double *a = (double *) malloc(n*sizeof(double)); if (a == NULL) errx(EXIT_FAILURE, "### error: can allocate %d double array", n); + int count = (int) ceil(fraction*n); for (i = 0; i < count; i++) a[i] = 0.1; for (i = count; i < n; i++) @@ -30,8 +32,11 @@ long count_if(double *x, int n, int k) { int i, j; for (j = 0; j < k; j++) for (i = 0; i < n; i++) - if (x[i] < 0.5) + if (x[i] < 0.5) { + x[i] = log(sqrt(x[i])); total++; + } else + x[i] = sqrt(-log(x[i])); return total; } @@ -39,14 +44,17 @@ long count_cond_expr(double *x, int n, int k) { long total = 0; int i, j; for (j = 0; j < k; j++) - for (i = 0; i < n; i++) + for (i = 0; i < n; i++) { total += x[i] < 0.5 ? 1 : 0; + x[i] = x[i] < 0.5 ? log(sqrt(x[i])) : sqrt(-log(x[i])); + } return total; } int main(int argc, char *argv[]) { int n = 100; int k = 100; + double fraction = 0.5; double *x; struct timeval tv1, tv2; long counter; @@ -54,6 +62,9 @@ int main(int argc, char *argv[]) { n = atoi(argv[1]); if (argc > 2) k = atoi(argv[2]); + if (argc > 3) + fraction = atof(argv[3]); + printf("random assignment:\n"); x = init(n); gettimeofday(&tv1, NULL); counter = count_if(x, n, k); @@ -61,6 +72,8 @@ int main(int argc, char *argv[]) { printf("if condition: %.6lf (%ld)\n", tv2.tv_sec - tv1.tv_sec + 1.0e-6*(tv2.tv_usec - tv1.tv_usec), counter); + free(x); + x = init(n); gettimeofday(&tv1, NULL); counter = count_cond_expr(x, n, k); gettimeofday(&tv2, NULL); @@ -68,7 +81,8 @@ int main(int argc, char *argv[]) { tv2.tv_sec - tv1.tv_sec + 1.0e-6*(tv2.tv_usec - tv1.tv_usec), counter); free(x); - x = init_cont(n, counter/k); + printf("%.3lf split:\n", fraction); + x = init_cont(n, fraction); gettimeofday(&tv1, NULL); counter = count_if(x, n, k); gettimeofday(&tv2, NULL); @@ -76,6 +90,8 @@ int main(int argc, char *argv[]) { tv2.tv_sec - tv1.tv_sec + 1.0e-6*(tv2.tv_usec - tv1.tv_usec), counter); gettimeofday(&tv1, NULL); + free(x); + x = init_cont(n, fraction); counter = count_cond_expr(x, n, k); gettimeofday(&tv2, NULL); printf("condition expression: %.6lf (%ld)\n",