-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
258 lines (202 loc) · 6.92 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <chrono>
#include <random>
#include <vector>
#include "poly.h"
#include "utils.h"
void bench_multiply() {
std::default_random_engine generator;
generator.seed(getNanoseconds());
std::uniform_int_distribution<int> degreeDistrib(0, 255);
std::vector<Poly> polys;
for (int i = 0; i < 1000; i++) {
polys.push_back(Poly::random(degreeDistrib(generator), generator));
}
// 1 - Check Correctness of karatsubas
{
int tries = 0;
int successes = 0;
for (Poly p : polys) {
for (Poly q : polys) {
if (p.degree() + q.degree() >= 256) {
continue;
}
tries ++;
Poly res1 = p.multiplyNaively(q);
Poly res2 = p.multiplyKaratsuba32(q);
if ((res1 + res2).size() == 0) {
successes ++;
}
}
}
std::cout << "Karatsuba32 success ratio : (" << successes << "/" << tries << ")" << std::endl;
}
// 2 - Bench the naive method
{
int forceBench = 0;
auto start = std::chrono::high_resolution_clock::now();
for (Poly p : polys) {
for (Poly q : polys) {
if (p.degree() + q.degree() >= 256) {
continue;
}
Poly r = p.multiplyNaively(q);
forceBench += r.degree();
}
}
auto end = std::chrono::high_resolution_clock::now();
volatile int forceBench2 = forceBench;
(void) forceBench2;
std::cout << "Naive took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
}
// 3 - Bench the karatsuba32 method
{
int forceBench = 0;
auto start = std::chrono::high_resolution_clock::now();
for (Poly p : polys) {
for (Poly q : polys) {
if (p.degree() + q.degree() >= 256) {
continue;
}
Poly r = p.multiplyKaratsuba32(q);
forceBench += r.degree();
}
}
auto end = std::chrono::high_resolution_clock::now();
volatile int forceBench2 = forceBench;
(void) forceBench2;
std::cout << "Karatsuba32 took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
}
}
Poly naiveShiftLeft(const Poly& p, int i) {
Poly res(p.size() + i);
for (unsigned j = 0; j < p.size(); j++) {
res.setBit(i + j, p.bit(j));
}
res.computeDegree();
return res;
}
Poly naiveShiftRight(const Poly& p, int i) {
Poly res(p.size() - i);
for (unsigned j = i; j < p.size(); j++) {
res.setBit(j - i, p.bit(j));
}
res.computeDegree();
return res;
}
void bench_shifts() {
std::default_random_engine generator;
generator.seed(getNanoseconds());
std::uniform_int_distribution<int> degreeDistrib(0, 255);
std::vector<Poly> polys;
for (int i = 0; i < 10000; i++) {
polys.push_back(Poly::random(degreeDistrib(generator), generator));
}
// 1 - Check Correctness of shifts
{
int tries = 0;
int successes = 0;
for (Poly p : polys) {
for (unsigned i = 0; i < 256 - p.size(); i++) {
tries ++;
Poly res1 = naiveShiftLeft(p, i);
Poly res2 = p << i;
if ((res1 + res2).size() == 0) {
successes ++;
}
}
for (unsigned i = 0; i <= p.size(); i++) {
tries ++;
Poly res1 = naiveShiftRight(p, i);
Poly res2 = p >> i;
if ((res1 + res2).size() == 0) {
successes ++;
}
}
}
std::cout << "Shifts success ratio : (" << successes << "/" << tries << ")" << std::endl;
}
// 2 - Bench the naive method
{
int forceBench = 0;
auto start = std::chrono::high_resolution_clock::now();
for (Poly p : polys) {
for (unsigned i = 0; i < 256 - p.size(); i++) {
Poly r = naiveShiftLeft(p, i);
forceBench += r.degree();
}
for (unsigned i = 0; i <= p.size(); i++) {
Poly r = naiveShiftRight(p, i);
forceBench += r.degree();
}
}
auto end = std::chrono::high_resolution_clock::now();
volatile int forceBench2 = forceBench;
(void) forceBench2;
std::cout << "Naive took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
}
// 3 - Bench the fast method
{
int forceBench = 0;
auto start = std::chrono::high_resolution_clock::now();
for (Poly p : polys) {
for (unsigned i = 0; i < 256 - p.size(); i++) {
Poly r = p << i;
forceBench += r.degree();
}
for (unsigned i = 0; i <= p.size(); i++) {
Poly r = p >> i;
forceBench += r.degree();
}
}
auto end = std::chrono::high_resolution_clock::now();
volatile int forceBench2 = forceBench;
(void) forceBench2;
std::cout << "Naive took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
}
}
void bench_division() {
std::default_random_engine generator;
generator.seed(getNanoseconds());
std::uniform_int_distribution<int> degreeDistrib(0, 255);
std::vector<Poly> polys;
for (int i = 0; i < 1000; i++) {
polys.push_back(Poly::random(degreeDistrib(generator), generator));
}
// 1 - Check Correctness of the division
{
int tries = 0;
int successes = 0;
for (Poly p1 : polys) {
for (Poly p2 : polys) {
tries ++;
Poly q, r;
p1.euclidianDivision(p2, q, r);
if ((p1 + q * p2 + r).size() == 0) {
successes ++;
}
}
}
std::cout << "Division success ratio : (" << successes << "/" << tries << ")" << std::endl;
}
// 2 - Bench the division
{
int forceBench = 0;
auto start = std::chrono::high_resolution_clock::now();
for (Poly p1 : polys) {
for (Poly p2 : polys) {
Poly q, r;
p1.euclidianDivision(p2, q, r);
forceBench += r.degree() + q.degree();
}
}
auto end = std::chrono::high_resolution_clock::now();
volatile int forceBench2 = forceBench;
(void) forceBench2;
std::cout << "Division took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
}
}
int main(){
bench_multiply();
bench_shifts();
bench_division();
}