-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
240 lines (194 loc) · 10.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "LinkedList.h"
#ifndef GCC_OPTIMIZATION
#define GCC_OPTIMIZATION 0
#endif
void insertLinkedList(LinkedList *list, double arr[], int n) {
for (int i = 0; i < n; i++) {
list->add(list, arr[i]);
}
}
void wcsInsertLinkedList(LinkedList *list, double arr[], int n) {
for (int i = 0; i <= n; i++) {
if (i % 2 == 0)
list->add(list, i);
}
for (int i = 0; i <= n; i++) {
if (i % 2 == 1)
list->add(list, i);
}
}
void destroyLinkedList(LinkedList *list, double arr[], int n) {
while (list->delete(list));
}
void iterLinkedList(LinkedList *list, double arr[], int n) {
for (Node node = list->getFirst(*list); !list->isDone(node); node = list->next(node)) {}
}
void sortLinkedList(LinkedList *list, double arr[], int n) {
list->sort(list);
}
void emptyFunction(double data) {}
void fastIterLinkedList(LinkedList *list, double arr[], int n) {
list->forEach(*list, emptyFunction);
}
double
benchmark(void (*f)(LinkedList *list, double arr[], int n), LinkedList *list, double arr[], int n, const char *text) {
clock_t startTime = clock();
f(list, arr, n);
clock_t endTime = clock();
double elapsedTimeSeconds = ((double) endTime - (double) startTime) / (double) CLOCKS_PER_SEC;
printf("%s = %f ms\n", text, elapsedTimeSeconds * 1E3);
return elapsedTimeSeconds;
}
float mean(const double array[], size_t size) {
double mean = 0;
for (int i = 0; i < size; i++) {
mean += array[i];
}
mean /= (float) size;
return mean;
}
float stdev(const double array[], size_t size) {
float mean_calc = mean(array, size);
float sigma = 0;
for (int i = 0; i < size; i++) {
sigma += powf((mean_calc - array[i]), 2);
}
return sqrtf(sigma / (float) size);
}
int main(int argc, char *argv[]) {
srand(clock());
// printf("%s\n", argv[1]);
// printf("%d\n", strcmp(argv[1], "-k") == 0);
if (argc == 6 && strcmp(argv[1], "-k") == 0){
int k = atoi(argv[2]);
int n = atoi(argv[3]);
int nb_exp = atoi(argv[4]);
int exp_num = atoi(argv[5]);
// printf("k=%d, n=%d, nb_exp=%d, exp_num=%d\n", k, n, nb_exp, exp_num);
FILE *k_file = fopen("../data/k.csv", "a");
double k_insertion_times[nb_exp];
double k_iteration_times[nb_exp];
double k_forEach_iteration_times[nb_exp];
double k_sort_times[nb_exp];
double k_destroy_times[nb_exp];
// printf("GCC OPTIMIZATION = %d\n\n", GCC_OPTIMIZATION);
for (int i = 0; i < nb_exp; ++i) {
// Create an array of random double elements
double array[n];
for (int j = 0; j < n; ++j) {
array[j] = 1000 * ((double) rand() / RAND_MAX);
}
LinkedList classicLl = initLinkedList(CLASSIC);
LinkedList unrolledLl = initLinkedList(UNROLLED);
k_insertion_times[i] = benchmark(insertLinkedList, &unrolledLl, array, n, "Unrolled Insertion time");
k_iteration_times[i] = benchmark(iterLinkedList, &unrolledLl, NULL, n,
"Unrolled Iteration time (Begin-End) ");
k_forEach_iteration_times[i] = benchmark(fastIterLinkedList, &unrolledLl, NULL, n,
"Unrolled Foreach Iteration time (Begin-End) ");
k_sort_times[i] = benchmark(sortLinkedList, &unrolledLl, NULL, n, "Unrolled Sort time (random) ");
k_destroy_times[i] = benchmark(destroyLinkedList, &unrolledLl, NULL, n, "Unrolled Destroy time ");
printf("\n");
}
fprintf(k_file, "%d,%s,%d,%f,%f\n", exp_num, "insertion", k, 1E3*mean(k_insertion_times, nb_exp),
1E3*stdev(k_insertion_times, nb_exp));
fprintf(k_file, "%d,%s,%d,%f,%f\n", exp_num, "iteration", k, 1E3*mean(k_iteration_times, nb_exp),
1E3*stdev(k_iteration_times, nb_exp));
fprintf(k_file, "%d,%s,%d,%f,%f\n", exp_num, "forEach-iteration", k, 1E3*mean(k_forEach_iteration_times, nb_exp),
1E3*stdev(k_forEach_iteration_times, nb_exp));
fprintf(k_file, "%d,%s,%d,%f,%f\n", exp_num, "sort", k, 1E3*mean(k_sort_times, nb_exp),
1E3*stdev(k_sort_times, nb_exp));
fprintf(k_file, "%d,%s,%d,%f,%f\n", exp_num, "destroy", k, 1E3*mean(k_destroy_times, nb_exp),
1E3*stdev(k_destroy_times, nb_exp));
} else if (argc == 4){
int n = atoi(argv[1]);
int nb_exp = atoi(argv[2]);
int exp_num = atoi(argv[3]);
// printf("n=%d, nb_exp=%d, exp_num=%d", n, nb_exp, exp_num);
FILE *insertion_file = fopen("../data/insertion.csv", "a");
FILE *iteration_file = fopen("../data/iteration.csv", "a");
FILE *foreach_iteration_file = fopen("../data/foreach_iteration.csv", "a");
FILE *destroy_file = fopen("../data/destroy.csv", "a");
FILE *sort_file = fopen("../data/sort.csv", "a");
double classic_insertion_times[nb_exp];
double unrolled_insertion_times[nb_exp];
double classic_iteration_times[nb_exp];
double unrolled_iteration_times[nb_exp];
double classic_forEach_iteration_times[nb_exp];
double unrolled_forEach_iteration_times[nb_exp];
double classic_sort_times[nb_exp];
double unrolled_sort_times[nb_exp];
double classic_destroy_times[nb_exp];
double unrolled_destroy_times[nb_exp];
// printf("GCC OPTIMIZATION = %d\n\n", GCC_OPTIMIZATION);
for (int i = 0; i < nb_exp; ++i) {
// Create an array of random double elements
double array[n];
for (int j = 0; j < n; ++j) {
array[j] = 1000 * ((double) rand() / RAND_MAX);
}
LinkedList classicLl = initLinkedList(CLASSIC);
classic_insertion_times[i] = benchmark(insertLinkedList, &classicLl, array, n, "Classic Insertion time");
LinkedList unrolledLl = initLinkedList(UNROLLED);
unrolled_insertion_times[i] = benchmark(insertLinkedList, &unrolledLl, array, n, "Unrolled Insertion time");
classic_iteration_times[i] = benchmark(iterLinkedList, &classicLl, NULL, n,
"Classic Iteration time (Begin-End) ");
unrolled_iteration_times[i] = benchmark(iterLinkedList, &unrolledLl, NULL, n,
"Unrolled Iteration time (Begin-End) ");
classic_forEach_iteration_times[i] = benchmark(fastIterLinkedList, &classicLl, NULL, n,
"Classic Foreach Iteration time (Begin-End) ");
unrolled_forEach_iteration_times[i] = benchmark(fastIterLinkedList, &unrolledLl, NULL, n,
"Unrolled Foreach Iteration time (Begin-End) ");
// classicLl.simplePrint(classicLl);
// unrolledLl.simplePrint(unrolledLl);
classic_sort_times[i] = benchmark(sortLinkedList, &classicLl, NULL, n, "Classic Sort time (random) ");
unrolled_sort_times[i] = benchmark(sortLinkedList, &unrolledLl, NULL, n, "Unrolled Sort time (random) ");
// classicLl.simplePrint(classicLl);
// unrolledLl.simplePrint(unrolledLl);
classic_destroy_times[i] = benchmark(destroyLinkedList, &classicLl, NULL, n, "Classic Destroy time ");
unrolled_destroy_times[i] = benchmark(destroyLinkedList, &unrolledLl, NULL, n, "Unrolled Destroy time ");
// classicLl.simplePrint(classicLl);
// unrolledLl.simplePrint(unrolledLl);
printf("\n");
}
const char *types[] = {"classic", "unrolled"};
for (int i = 0; i < 2; ++i) {
char typeOptimization[20] = "";
strcat(typeOptimization, types[i]);
strcat(typeOptimization, "-Ofast");
const char *type = GCC_OPTIMIZATION ? typeOptimization : types[i];
// printf("%s \n", type);
if (i == 0) {
fprintf(insertion_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(classic_insertion_times, nb_exp),
1E3*stdev(classic_insertion_times, nb_exp));
fprintf(iteration_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(classic_iteration_times, nb_exp),
1E3*stdev(classic_iteration_times, nb_exp));
fprintf(foreach_iteration_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(classic_forEach_iteration_times, nb_exp),
1E3*stdev(classic_forEach_iteration_times, nb_exp));
fprintf(sort_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(classic_sort_times, nb_exp),
1E3*stdev(classic_sort_times, nb_exp));
fprintf(destroy_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(classic_destroy_times, nb_exp),
1E3*stdev(classic_destroy_times, nb_exp));
} else {
fprintf(insertion_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(unrolled_insertion_times, nb_exp),
1E3*stdev(unrolled_insertion_times, nb_exp));
fprintf(iteration_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(unrolled_iteration_times, nb_exp),
1E3*stdev(unrolled_iteration_times, nb_exp));
fprintf(foreach_iteration_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(unrolled_forEach_iteration_times, nb_exp),
1E3*stdev(unrolled_forEach_iteration_times, nb_exp));
fprintf(sort_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(unrolled_sort_times, nb_exp),
1E3*stdev(unrolled_sort_times, nb_exp));
fprintf(destroy_file, "%d,%s,%d,%f,%f\n", exp_num, type, n, 1E3*mean(unrolled_destroy_times, nb_exp),
1E3*stdev(unrolled_destroy_times, nb_exp));
}
}
} else {
printf("Usage: %s <elements-number> <nb-experiments> <#experimento> (see run.sh)\n", argv[0]);
exit(-1);
}
return 0;
}