-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector-testing.cpp
174 lines (147 loc) · 4.21 KB
/
vector-testing.cpp
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
#include <vector>
#include <time.h>
#include <set>
// TODO: insert sorted
// TODO: gtime -v run it with add it to all the test cases
// once for each structure
// given a starting size, and a total number of elements to insert
// create a vector that will take all of these elements
// inserts in random places
void testInsertRandom(int beginSize, int size){
//printf("Insert %d elements randomly into vector, begin size: %d \n", size, beginSize);
clock_t start, end;
double cpu_time_used = 0;
int currentSize = beginSize;
std::vector<int> vec (beginSize , size);
std::vector<int>::iterator it;
for(int i=0; i<size; i++){
int vecSize = vec.size();
int index = rand() % vecSize;
it = (vec.begin()+index);
start = clock();
vec.insert(it, i);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
printf("%d, %f\n", size, cpu_time_used);
//printf("%f for %d inserts\n", cpu_time_used, size);
}
// inserting elements near each other
void testInsertNear(int size){
std::vector<int> vec (10 , size);
std::vector<int>::iterator it;
int last_index = 0;
for(int i=0; i<size; i++){
// random comput stuff to make sure our inserts are close to eachother
int vecSize = vec.size();
int index = last_index + (rand() % 5);
if(index%2==1){
index = last_index - (rand() % 5);
if(index<0){
index = -index; // don't want to insert negative index
}
}
it = (vec.begin()+index);
vec.insert(it, i);
}
}
void testInsertVarious(){
printf("Tests Vector Inserts for sizes 10, 100, 1,000, 10,000, 100,000 \n\n");
testInsertRandom(10, 10);
testInsertRandom(10, 100);
testInsertRandom(10, 1000);
testInsertRandom(10, 10000);
testInsertRandom(10, 100000);
testInsertRandom(10, 1000000);
}
void testInsertSet(int beginSize, int size){
clock_t start, end;
double cpu_time_used = 0;
int currentSize = beginSize;
std::set<int> myset;
start = clock();
for(int i=0; i<size; i++){
myset.insert(i);
}
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
printf("%d, %f\n", size, cpu_time_used);
}
// insert random elements into set
// set should order it for us
void testInsertSortedRandomSet(int beginSize, int size){
clock_t start, end;
double cpu_time_used = 0;
int currentSize = beginSize;
std::set<int> myset;
start = clock();
for(int i=0; i<size; i++){
//int elem = rand()%100;
myset.insert(i);
}
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
printf("%d, %f\n", size, cpu_time_used);
}
void testMaxVector(int size){
clock_t start, end;
double cpu_time_used = 0;
std::vector<int> vec(10, size);
std::vector<int>::iterator it;
for(int i=0; i<size; i++){
int vecSize = vec.size();
int index = rand() % vecSize;
it = (vec.begin()+index);
vec.insert(it, i);
}
printf("inserted");
int max_elem = -1;
start = clock();
for(int i=0; i<size; i++){
if(vec[i]>max_elem){
max_elem = vec[i];
}
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("size:%d, time:%f, max:%d, \n", size, cpu_time_used, max_elem);
}
void testScanSet(int size){
clock_t start, end;
double cpu_time_used = 0;
std::set<int> myset;
std::set<int>::iterator it;
for(int i=0; i<size; i++){
myset.insert(i);
}
int max_elem = -1;
start = clock();
for (it = myset.begin(); it != myset.end(); ++it){
int elem = *it; // Note the "*" here
if(*it>max_elem){
max_elem=*it;
}
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("size:%d, time:%f, max_elem:%d\n", size, cpu_time_used, max_elem);
}
int main(){
//testInsertVarious();
// printf("set\n");
// testScanSet(1000);
// testScanSet(10000);
// testScanSet(100000);
// testScanSet(1000000);
// printf("vector\n");
// testMaxVector(1000);
// testMaxVector(10000);
// testMaxVector(100000);
// // testMaxVector(1000000);
// testInsertSortedRandomSet(10, 10);
// testInsertSortedRandomSet(10, 100);
// testInsertSortedRandomSet(10, 1000);
// testInsertSortedRandomSet(10, 10000);
testInsertSortedRandomSet(10, 1000);
// testInsertNear(1000000);
}