-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlhr_int_checker.cpp
125 lines (108 loc) · 3.92 KB
/
lhr_int_checker.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
#include "Fraction.hpp"
#include "boost/multiprecision/cpp_int.hpp"
#include <ctime>
#include <csignal>
#include <chrono>
#include <thread>
#include <string>
#include <random>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdexcept>
bool running = 1;
int lim_a, lim_b, count, interval;
void sig_handler(int signal){
if(running){
std::cout << "\nPress Ctrl+C Again to Exit\n" << std::endl;
running = 0;
}else{
exit(0);
}
}
void gen_log(const std::string& a, const std::string& b, const std::ostringstream& c1, const std::ostringstream& c2){
std::ofstream ofs("Data.in");
ofs << a << '\n' << b << std::endl;
ofs.close(), ofs.open("LHR.out");
ofs << c1.str() << std::endl;
ofs.close(), ofs.open("Boost.out");
ofs << c2.str() << std::endl;
ofs.close();
}
void lhr_calc(const Int& lhr_a, const Int& lhr_b, std::ostringstream& c1, bool opt){
auto start = std::chrono::high_resolution_clock::now();
Int lhr_c;
if(opt)lhr_c = lhr_a / lhr_b;
else lhr_c = lhr_a * lhr_b;
auto end = std::chrono::high_resolution_clock::now();
c1 << lhr_c;
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "LHR a" << (opt ? '/' : '*') << "b Used Time: " << duration.count() << " ms" << std::endl;
}
void boost_calc(const boost::multiprecision::cpp_int& boost_a, const boost::multiprecision::cpp_int& boost_b,
std::ostringstream& c2, bool opt){
auto start = std::chrono::high_resolution_clock::now();
boost::multiprecision::cpp_int boost_c;
if(opt)boost_c = boost_a / boost_b;
else boost_c = boost_a * boost_b;
auto end = std::chrono::high_resolution_clock::now();
c2 << boost_c;
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Boost a" << (opt ? '/' : '*') << "b Used Time: " << duration.count() << " ms" << std::endl;
}
int main(int argc, char **argv){
if(argc != 5)throw std::runtime_error("Please Provide 4 Args: [a_limit] [b_limit] [count] [interval_in_ms]");
try{
lim_a = std::stoi(std::string(argv[1]));
lim_b = std::stoi(std::string(argv[2]));
count = std::stoi(std::string(argv[3]));
interval = std::stoi(std::string(argv[4]));
if(lim_a <= 0 || lim_b <= 0 || count <= 0 || interval < 0)throw::std::runtime_error("Invalid Args");
}catch (const std::exception &e){
std::cerr << e.what() << std::endl;
return 1;
}
std::mt19937 rnd(time(0));
std::uniform_int_distribution<int> range_a(1, lim_a), range_b(1, lim_b);
std::uniform_int_distribution<int> range_digit('0', '9');
std::cout << "Start Testing..." << std::endl;
std::signal(SIGINT, sig_handler);
for(int i = 0; i < count; i++){
std::cout << "Start Round #" << i + 1 << std::endl;
int len_a = range_a(rnd);
int len_b = range_b(rnd);
std::string a = std::to_string(range_digit(rnd));
std::string b = std::to_string(range_digit(rnd));
std::ostringstream c1, c2;
while(a[0] == '0')a[0] = range_digit(rnd);
while(b[0] == '0')b[0] = range_digit(rnd);
for(int j = 1; j < len_a; j++)a += range_digit(rnd);
for(int j = 1; j < len_b; j++)b += range_digit(rnd);
Int lhr_a, lhr_b;
std::istringstream iss_a(a), iss_b(b);
iss_a >> lhr_a, iss_b >> lhr_b;
lhr_calc(lhr_a, lhr_b, c1, 0);
boost::multiprecision::cpp_int boost_a(a), boost_b(b);
boost_calc(boost_a, boost_b, c2, 0);
if(c1.str() != c2.str()){
std::cout << "(a*b) Product Not Match (YJX_AK_IOI)" << std::endl;
std::cout << "Refer to Data.in/LHR.out/Boost.out for Detail" << std::endl;
gen_log(a, b, c1, c2);
return 2;
}
c1.clear(), c2.clear();
lhr_calc(lhr_a, lhr_b, c1, 1);
boost_calc(boost_a, boost_b, c2, 1);
if(c1.str() != c2.str()){
std::cout << "(a/b) Product Not Match (WTY_AK_IOI)" << std::endl;
std::cout << "Refer to Data.in/LHR.out/Boost.out for Detail" << std::endl;
gen_log(a, b, c1, c2);
return 3;
}
if(i != count - 1){
std::cout << "Sleeping...\n" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
}
return 0;
}