-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray-doubling-testing.c
219 lines (176 loc) · 4.73 KB
/
array-doubling-testing.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
#include "array_doubling.c"
#include <time.h>
void testInsertGtime(int size){
list_t list;
setup(&list);
for(int i=0; i< size; i++){
insert(&list, i,i);
}
}
// inserts N elements sequentially into ofm.
// prints out time it takes for N operations
list_t testSequentialInsert(int size){
list_t list;
setup(&list);
//print_array(&list);
clock_t start, end;
double cpu_time_used = 0;
for(int i=0; i<size; i++){
//start = clock();
insert(&list, i,i);
//end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
//print_array(&list);
printf("%d sequential insertions, time: %f \n", size, cpu_time_used);
return list;
}
// inserts N elements at random locations into OFM.
// prints out time it takes for N operations
list_t testRandomInsert(int size){
list_t list;
setup(&list);
//print_array(&list);
clock_t start, end;
double cpu_time_used = 0;
for(int i=0; i<size; i++){
int index = rand()%size;
//start = clock();
insert(&list,index,i);
//end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
//print_array(&list);
printf("%d random insertions, time: %f \n", size, cpu_time_used);
return list;
}
void testInsertSortedReverse(int size){
printf("Testing Reverse Insert Sort:\n");
list_t list;
setup(&list);
clock_t start, end;
double cpu_time_used = 0;
// insert item elements in reverse order
for(int i=size-1; i>=0; i--){
start = clock();
insert_sorted(&list, i);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
// for correction
for(int i=0; i<size-1; i++){
int cur_val = list.items[i];
int next_val = list.items[i+1];
if(next_val<cur_val){
printf("insert ordered not working!");
break;
}
}
free(list.items);
printf("%d reverse sequential insertions for insert-ordered, time: %f \n", size, cpu_time_used);
}
void testInsertSortedRandom(int size){
// printf("Testing Random Insert Sorted:\n");
list_t list;
setup(&list);
clock_t start, end;
double cpu_time_used = 0;
start = clock();
for(int i=0; i<size; i++){
int val = rand()%100;
insert_sorted(&list, val);
}
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
// for correction
for(int i=0; i<size-1; i++){
int cur_val = list.items[i];
int next_val = list.items[i+1];
if(next_val<cur_val){
printf("insert ordered not working!");
break;
}
}
//printf("%d random insertions for insert-ordered, time: %f \n", size, cpu_time_used);
printf("%d, %f, \n", size, cpu_time_used);
free(list.items);
}
// inserting at most 5 away or near at a time
void testInsertSortedNear(int size){
printf("Testing Random Insert Sorted Near:\n");
list_t list;
setup(&list);
clock_t start, end;
double cpu_time_used = 0;
int lastVal = 0;
//start = clock();
for(int i=0; i<size; i++){
int dev = rand()%5;
int val = lastVal + dev;
if(dev%2==1){
val = abs(lastVal - dev);
}
insert_sorted(&list, val);
}
//end = clock();
//cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
// for correction
for(int i=0; i<size-1; i++){
int cur_val = list.items[i];
int next_val = list.items[i+1];
if(next_val<cur_val){
printf("insert ordered not working!");
break;
}
}
printf("%d random insertions for insert-ordered, time: %f \n", size, cpu_time_used);
free(list.items);
}
void testInsertSortedRandomVarious(){
//printf("Tests Array Insert Sorted Random sizes 10, 100, 1,000, 10,000, 100,000 \n\n");
testInsertSortedRandom(10);
testInsertSortedRandom(100);
testInsertSortedRandom(1000);
testInsertSortedRandom(10000);
testInsertSortedRandom(100000);
testInsertSortedRandom(1000000);
}
void testInsertSortedNearVarious(){
printf("Tests Array Insert Sorted Near sizes 10, 100, 1,000, 10,000, 100,000 \n\n");
testInsertSortedNear(10);
testInsertSortedNear(100);
testInsertSortedNear(1000);
testInsertSortedNear(10000);
testInsertSortedNear(100000);
testInsertSortedNear(1000000);
}
void testMax(int size){
list_t list;
clock_t start, end;
setup(&list);
for(int i=0; i<size; i++){
int val = rand()%100;
insert_sorted(&list, val);
}
start = clock();
int max_elem = max(&list);
end = clock();
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("max:%d, %d, time: %f \n",max_elem, size, cpu_time_used);
free(list.items);
}
int main(){
int size=100000;
//printf("Beginning Testing Suite for Array Doubling. N is: %d\n", size);
//testSequentialInsert(size);
//list_t list = testRandomInsert(size);
//testFindMax(&list, size);
// testInsertSortedReverse(size);
// testInsertSortedRandom(size);
testInsertSortedRandomVarious();
//testInsertSortedNear(1000000);
// testMax(1000);
// testMax(10000);
// testMax(100000);
// testMax(1000000);
}