-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.cpp
233 lines (207 loc) · 6.17 KB
/
test.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
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
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <windows.h>
#include "biginteger.h"
using namespace std;
typedef long long Type;
const int TESTS = 80000;
const char typefile[] = "type.txt";
const char bigfile[] = "big.txt";
const Type ZeroType = 0;
const BigInteger ZeroBig = 0;
Type type[TESTS + 5];
BigInteger big[TESTS + 5];
void Rand(bool half = false, bool Sqrt = false);
void Output();
void Input();
void Absolute();
void Compare_Calculate();
int main() {
ios::sync_with_stdio(false);
srand((unsigned)time(NULL));
Rand();
Output();
Input();
Absolute();
Compare_Calculate();
return 0;
}
void Rand(bool half, bool Sqrt) {
Type tmp;
for (int i = 0; i < TESTS; ++i) {
do {
tmp = ((Type)rand()) << (rand() % (sizeof(Type) * 4));
if(rand() % 2 == 1) {
tmp *= ((Type)rand()) << (rand() % (sizeof(Type) * 4));
}
if(half) {
tmp = abs(tmp / 2) - 1;
}
if(Sqrt) {
if(abs(tmp) < 0) {
--tmp;
}
tmp = sqrt(abs(tmp)) - 1;
}
if(rand() % 2 == 1) {
tmp = -tmp;
}
} while(tmp == 0);
type[i] = tmp;
big[i] = tmp;
}
}
void Output() {
ofstream fout;
int cnt;
clock_t start, stop;
double time_of_type, time_of_big;
fout.open(typefile);
cnt = 0;
start = clock();
while(cnt != TESTS) {
fout << type[cnt++] << endl;
}
fout << ZeroType << endl;
stop = clock();
fout.close();
time_of_type = stop - start;
fout.open(bigfile);
cnt = 0;
start = clock();
while(cnt != TESTS) {
fout << big[cnt++] << endl;
}
fout << ZeroBig << endl;
stop = clock();
fout.close();
time_of_big = stop - start;
cout << "Output time of type: " << time_of_type << endl
<< "Output time of big: " << time_of_big << endl
<< "time_of_big / time_of_type = " << time_of_big / time_of_type << endl << endl;
system((string("fc ") + typefile + " " + bigfile).c_str());
}
void Input() {
ifstream fin;
Type typetmp;
BigInteger bigtmp;
clock_t start, stop;
double time_of_type, time_of_big;
fin.open(typefile);
start = clock();
while(fin >> typetmp);
stop = clock();
fin.close();
time_of_type = stop - start;
fin.open(bigfile);
start = clock();
while(fin >> bigtmp);
stop = clock();
fin.close();
time_of_big = stop - start;
cout << "Input time of type: " << time_of_type << endl
<< "Input time of big: " << time_of_big << endl
<< "time_of_big / time_of_type = " << time_of_big / time_of_type << endl << endl;
}
void Absolute() {
int cnt;
ofstream fout;
clock_t start, stop;
double time_of_type, time_of_big;
fout.open(typefile);
cnt = 0;
start = clock();
while(cnt != TESTS) {
if(type[cnt] == BigInteger::LONG_LONG_MIN) {
fout << "9223372036854775808" << endl;
} else if(type[cnt] == INT_MIN) {
fout << "2147483648" << endl;
} else {
fout << abs(type[cnt]) << endl;
}
++cnt;
}
fout << abs(ZeroType) << endl;
stop = clock();
fout.close();
time_of_type = stop - start;
fout.open(bigfile);
cnt = 0;
start = clock();
while(cnt != TESTS) {
fout << big[cnt].abs() << endl;
++cnt;
}
fout << ZeroBig.abs() << endl;
stop = clock();
fout.close();
time_of_big = stop - start;
cout << "Abs time of type: " << time_of_type << endl
<< "Abs time of big: " << time_of_big << endl
<< "time_of_big / time_of_type = " << time_of_big / time_of_type << endl << endl;
system((string("fc ") + typefile + " " + bigfile).c_str());
}
template<typename T>
void cmpcal(T *arr, int command, ofstream &fout) {
int cnt;
const T ZERO = 0;
cnt = 0;
while(cnt != TESTS) {
switch(command) {
case 1: {
fout << (arr[cnt] == arr[cnt + 1]) << endl
<< (arr[cnt] != arr[cnt + 1]) << endl
<< (arr[cnt] < arr[cnt + 1]) << endl
<< (arr[cnt] <= arr[cnt + 1]) << endl
<< (arr[cnt] > arr[cnt + 1]) << endl
<< (arr[cnt] >= arr[cnt + 1]) << endl;
break;
}
case 2: fout << arr[cnt] / arr[cnt + 1] << endl; break;
case 3: fout << arr[cnt] % arr[cnt + 1] << endl; break;
case 4: fout << arr[cnt] + arr[cnt + 1] << endl; break;
case 5: fout << arr[cnt] - arr[cnt + 1] << endl; break;
case 6: fout << arr[cnt] * arr[cnt + 1] << endl; break;
default: break;
}
cnt += 2;
}
switch(command) {
case 2: fout << ZERO / arr[0] << endl; break;
case 3: fout << ZERO % arr[0] << endl; break;
case 4: fout << ZERO + arr[0] << endl << arr[0] + ZERO << endl; break;
case 5: fout << ZERO - arr[0] << endl << arr[0] - ZERO << endl; break;
case 6: fout << ZERO * arr[0] << endl << arr[0] * ZERO << endl; break;
default: break;
}
}
void Compare_Calculate() {
ofstream fout;
clock_t start, stop;
double time_of_type, time_of_big;
string command[6] = {"Compare", "Div", "Mod", "Add", "Cut", "Multiply"};
for(int i = 1; i <= 6; ++i) {
switch(i) {
case 4: Rand(true, false); break;
case 6: Rand(false, true); break;
default: break;
}
fout.open(typefile);
start = clock();
cmpcal(type, i, fout);
stop = clock();
fout.close();
time_of_type = stop - start;
fout.open(bigfile);
start = clock();
cmpcal(big, i, fout);
stop = clock();
fout.close();
time_of_big = stop - start;
cout << command[i - 1] << " time of type: " << time_of_type << endl
<< command[i - 1] << " time of big: " << time_of_big << endl
<< "time_of_big / time_of_type = " << time_of_big / time_of_type << endl << endl;
system((string("fc ") + typefile + " " + bigfile).c_str());
}
}