-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (138 loc) · 3.83 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (138 loc) · 3.83 KB
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
#include <atomic>
#include <sstream>
#include <vector>
#include <fstream>
#include <iostream>
#include "methods.hpp"
#include "main.hpp"
/*
* Arguments: method# infile outfile
*/
int
main (int argc, char **argv) {
if (argc < 4)
{
std::cerr << "Wrong number of arguments" << std::endl;
return WRONG_ARGS;
}
std::string TIME_RES_FN = ".temp"; // Should be the same as TIME_RES_FN in python script
// TIME_RES_FN can be passed as fifth argument. Defaults to the line above if not specified
if (argc == 5)
TIME_RES_FN = argv[4];
int method = atoi(argv[1]);
std::string in_file = argv[2];
std::string out_file = argv[3];
std::vector<double> numbers;
auto r_status = read_numbers(in_file, &numbers);
if (r_status) {
std::cerr << "Error writing results" << std::endl;
return r_status;
}
std::pair<size_t, double> res;
auto start_time = get_current_time_fenced();
switch (method) {
case 1:
res = method1_stringstream(numbers);
break;
case 2:
res = method2_to_string(numbers);
break;
case 3:
res = method3_sprintf(numbers);
break;
case 4:
res = method4_boost(numbers);
break;
case 5:
res = method5_qt(numbers);
break;
case 6:
res = method6_ryu(numbers);
break;
case 7:
res = method7_google(numbers);
break;
case 8:
res = method8_gcvt(numbers);
break;
case 9:
res = method9_ostringstream(numbers);
break;
case 10:
res = method10_strtk(numbers);
break;
case 11:
res = method11_custom(numbers);
break;
case 12:
res = method12_write_to_file(numbers);
break;
case 13:
res = method13_to_wstring(numbers);
break;
case 14:
res = method14_floaxie(numbers);
break;
default:
{
std::cerr << "Wrong method" << std::endl;
return WRONG_METHOD;
}
}
auto finish_time = get_current_time_fenced();
//std::cout << res.first << " " << res.second << std::endl;
std::ofstream time_res(TIME_RES_FN);
time_res << to_us(finish_time - start_time);
time_res << "\n";
time_res.close();
auto w_status = write_res(out_file, res);
if (w_status) {
std::cerr << "Error writing results" << std::endl;
return w_status;
}
return 0;
}
int read_numbers(const std::string &fn, std::vector<double> *numbers)
{
std::ifstream file(fn);
if (!file)
{
std::cerr << "Error opening in file" << std::endl;
return ERROR_OPENING_IN_FILE;
}
double current_word = 0;
while(file>>current_word)
(*numbers).push_back(current_word);
if (!file.eof())
{
std::cerr << "Error reading in file" << std::endl;
return ERROR_READING_IN_FILE;
}
return 0;
}
int
write_res (const std::string &fn, std::pair<size_t, double> res)
{
std::ofstream file(fn);
if (!file)
{
std::cerr << "Error opening out file" << std::endl;
return ERROR_OPENING_OUT_FILE;
}
std::stringstream res_ss;
res_ss << res.first << "\n" << res.second << "\n";
file << res_ss.str();
return 0;
}
template<class D>
inline long long to_us(const D& d)
{
return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
}
inline std::chrono::high_resolution_clock::time_point get_current_time_fenced()
{
std::atomic_thread_fence(std::memory_order_seq_cst);
auto res_time = std::chrono::high_resolution_clock::now();
std::atomic_thread_fence(std::memory_order_seq_cst);
return res_time;
}