-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmeasure.cpp
136 lines (111 loc) · 3.43 KB
/
measure.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
/* Программа, которая формирует списки данных по
определенным критериям и запускает на них различные сортировки
для сравнения и подсчета статистики
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "sort.h"
#include "../libcpp/measurer.h"
using namespace std;
struct MeasureData {
string sort;
string type;
int size;
double timeInSeconds;
long assignments;
long comparsions;
};
static long comparsions;
static long assignments;
static vector<MeasureData> md;
class TestInt {
public:
int value;
TestInt& operator= (const TestInt& rhs) {
assignments++;
this->value = rhs.value;
return *this;
}
TestInt& operator= (const int i) {
assignments++;
this->value = i;
return *this;
}
friend bool operator> (const TestInt& i1, const TestInt& i2);
friend bool operator>= (const TestInt& i1, const TestInt& i2);
friend bool operator< (const TestInt& i1, const TestInt& i2);
friend bool operator<= (const TestInt& i1, const TestInt& i2);
};
bool operator> (const TestInt& i1, const TestInt& i2) {
comparsions++;
return i1.value > i2.value;
}
bool operator>= (const TestInt& i1, const TestInt& i2) {
comparsions++;
return i1.value >= i2.value;
}
bool operator< (const TestInt& i1, const TestInt& i2) {
comparsions++;
return i1.value < i2.value;
}
bool operator<= (const TestInt& i1, const TestInt& i2) {
comparsions++;
return i1.value <= i2.value;
}
void resetCounters() {
assignments = 0;
comparsions = 0;
}
void measure(const string& sort, const string& type, int size) {
TestInt* data = new TestInt[size];
if (type == "random") {
for (int i = 0; i < size; i++)
data[i].value = rand();
} else if (type == "sorted") {
for (int i = 0; i < size; i++)
data[i].value = i;
} else if (type == "revers") {
for (int i = 0; i < size; i++)
data[i] = size - i;
}
resetCounters();
MeasureData dt;
dt.sort = sort;
dt.type = type;
dt.size = size;
measurer m;
if (sort == "quick") {
quicksort(data, size);
} else if (sort == "merge") {
mergesort(data, size);
} else if (sort == "heap") {
heapsort(data, size);
}
dt.timeInSeconds = m.seconds();
dt.assignments = assignments;
dt.comparsions = comparsions;
md.push_back(dt);
delete [] data;
}
int main(int argc, const char* argv[]) {
string sorts[] = {"quick", "merge", "heap"};
const int sortsLen = sizeof(sorts)/sizeof(string);
string types[] = {"random", "sorted", "revers"};
const int typesLen = sizeof(types)/sizeof(string);
int sizes[] = {10000000};
const int sizesLen = sizeof(sizes)/sizeof(int);
for (int is = 0; is < sortsLen; ++is)
for (int it = 0; it < typesLen; ++it)
for (int sz = 0; sz < sizesLen; ++sz)
measure(sorts[is], types[it], sizes[sz]);
cout << "sort" << "\t" << "type" << "\t" << "size" << "\t"
<< "sec" << "\t" << "assign" << "\t" << "compar"
<< "\t"<< "total" << endl;
for (vector<MeasureData>::iterator it = md.begin(); it < md.end(); ++it)
cout << it->sort << "\t" << it->type << "\t" << it->size << "\t"
<< it->timeInSeconds << "\t" << it->assignments << "\t" << it->comparsions
<< "\t"<< it->assignments + it->comparsions << "\t" << (it->assignments + it->comparsions) / it->size << endl;
return 0;
}