Skip to content

Commit 9d10b70

Browse files
authored
Merge pull request #8 from gjbex/development
Add discussion of pipelines and branches
2 parents 2382047 + 0c657c4 commit 9d10b70

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

code_optimization.pptx

7.44 KB
Binary file not shown.

source-code/Branching/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
CFLAGS = -O2 -g -Wall -Wextra
2+
LDLIBS = -lm
23

34
all: branch.exe
45

56
%.exe: %.c
6-
$(CC) $(CFLAGS) -o $@ $<
7+
$(CC) $(CFLAGS) -o $@ $< $(LDLIBS)
78

89
clean:
910
$(RM) *.exe *.o

source-code/Branching/branch.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <err.h>
2+
#include <math.h>
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <sys/time.h>
@@ -13,11 +14,12 @@ double *init(int n) {
1314
return a;
1415
}
1516

16-
double *init_cont(int n, int count) {
17+
double *init_cont(int n, double fraction) {
1718
int i;
1819
double *a = (double *) malloc(n*sizeof(double));
1920
if (a == NULL)
2021
errx(EXIT_FAILURE, "### error: can allocate %d double array", n);
22+
int count = (int) ceil(fraction*n);
2123
for (i = 0; i < count; i++)
2224
a[i] = 0.1;
2325
for (i = count; i < n; i++)
@@ -30,52 +32,66 @@ long count_if(double *x, int n, int k) {
3032
int i, j;
3133
for (j = 0; j < k; j++)
3234
for (i = 0; i < n; i++)
33-
if (x[i] < 0.5)
35+
if (x[i] < 0.5) {
36+
x[i] = log(sqrt(x[i]));
3437
total++;
38+
} else
39+
x[i] = sqrt(-log(x[i]));
3540
return total;
3641
}
3742

3843
long count_cond_expr(double *x, int n, int k) {
3944
long total = 0;
4045
int i, j;
4146
for (j = 0; j < k; j++)
42-
for (i = 0; i < n; i++)
47+
for (i = 0; i < n; i++) {
4348
total += x[i] < 0.5 ? 1 : 0;
49+
x[i] = x[i] < 0.5 ? log(sqrt(x[i])) : sqrt(-log(x[i]));
50+
}
4451
return total;
4552
}
4653

4754
int main(int argc, char *argv[]) {
4855
int n = 100;
4956
int k = 100;
57+
double fraction = 0.5;
5058
double *x;
5159
struct timeval tv1, tv2;
5260
long counter;
5361
if (argc > 1)
5462
n = atoi(argv[1]);
5563
if (argc > 2)
5664
k = atoi(argv[2]);
65+
if (argc > 3)
66+
fraction = atof(argv[3]);
67+
printf("random assignment:\n");
5768
x = init(n);
5869
gettimeofday(&tv1, NULL);
5970
counter = count_if(x, n, k);
6071
gettimeofday(&tv2, NULL);
6172
printf("if condition: %.6lf (%ld)\n",
6273
tv2.tv_sec - tv1.tv_sec + 1.0e-6*(tv2.tv_usec - tv1.tv_usec),
6374
counter);
75+
free(x);
76+
x = init(n);
6477
gettimeofday(&tv1, NULL);
6578
counter = count_cond_expr(x, n, k);
6679
gettimeofday(&tv2, NULL);
6780
printf("condition expression: %.6lf (%ld)\n",
6881
tv2.tv_sec - tv1.tv_sec + 1.0e-6*(tv2.tv_usec - tv1.tv_usec),
6982
counter);
7083
free(x);
71-
x = init_cont(n, counter/k);
84+
printf("%.3lf split:\n", fraction);
85+
x = init_cont(n, fraction);
7286
gettimeofday(&tv1, NULL);
7387
counter = count_if(x, n, k);
7488
gettimeofday(&tv2, NULL);
7589
printf("if condition: %.6lf (%ld)\n",
7690
tv2.tv_sec - tv1.tv_sec + 1.0e-6*(tv2.tv_usec - tv1.tv_usec),
7791
counter);
7892
gettimeofday(&tv1, NULL);
93+
free(x);
94+
x = init_cont(n, fraction);
7995
counter = count_cond_expr(x, n, k);
8096
gettimeofday(&tv2, NULL);
8197
printf("condition expression: %.6lf (%ld)\n",

0 commit comments

Comments
 (0)