-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
320 lines (250 loc) · 10.7 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//benchmark copied from https://cedardb.com/blog/exceptions_vs_errors/
#include <benchmark/benchmark.h>
#include "match.hpp"
#include <expected>
#include <string>
#include <sstream>
#include <vector>
#include <random>
#include <charconv>
#include <cassert>
#include <iostream>
using namespace cppmatch;
struct invalid_value { std::string reason; };
unsigned do_fib_throws(unsigned n, unsigned max_depth) {
if (!max_depth) throw invalid_value(std::to_string(n) + " exceeds max_depth");
if (n <= 2) return 1;
return do_fib_throws(n - 2, max_depth - 1) + do_fib_throws(n - 1, max_depth - 1);
}
std::expected<unsigned, invalid_value> do_fib_expected(unsigned n, unsigned max_depth) {
if (!max_depth) return std::unexpected<invalid_value>(std::to_string(n) + " exceeds max_depth");
if (n <= 2) return 1;
auto n2 = do_fib_expected(n - 2, max_depth - 1);
if (!n2) return std::unexpected(n2.error());
auto n1 = do_fib_expected(n - 1, max_depth - 1);
if (!n1) return std::unexpected(n1.error());
return *n1 + *n2;
}
Result<unsigned, invalid_value> do_fib_cppmatch(unsigned n, unsigned max_depth) {
if (!max_depth) return invalid_value{std::to_string(n) + " exceeds max_depth"};
if (n <= 2) return 1U;
return expect(do_fib_cppmatch(n - 2, max_depth - 1)) + expect(do_fib_cppmatch(n - 1, max_depth - 1));
}
Result<unsigned, invalid_value> do_fib_cppmatch_with_exceptions(unsigned n, unsigned max_depth) {
if (!max_depth) return invalid_value{std::to_string(n) + " exceeds max_depth"};
if (n <= 2) return 1U;
return expect_e(do_fib_cppmatch_with_exceptions(n - 2, max_depth - 1)) + expect_e(do_fib_cppmatch_with_exceptions(n - 1, max_depth - 1));
}
static void recursive_fib_std_expected(benchmark::State& state) {
for (auto _ : state) {
auto res = do_fib_expected(15, 20);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(recursive_fib_std_expected);
static void recursive_fib_cppmatch(benchmark::State& state) {
for (auto _ : state) {
auto res = do_fib_cppmatch(15, 20);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(recursive_fib_cppmatch);
static void recursive_fib_cppmatch_with_exceptions(benchmark::State& state) {
for (auto _ : state) {
auto res = match_e(do_fib_cppmatch_with_exceptions(15, 20), [](auto&&){return "";});
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(recursive_fib_cppmatch_with_exceptions);
static void recursive_fib_throws(benchmark::State& state) {
for (auto _ : state) {
auto res = do_fib_throws(15, 20);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(recursive_fib_throws);
//=== Coordinate Parsing Benchmarks ===
std::string generate_random_coordinate_string() {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_real_distribution<> prob_dist(0.0, 1.0);
double probability = prob_dist(gen);
if (probability < 0.9) { // 90% chance of valid coordinates
std::uniform_real_distribution<> lat_dist(-90.0, 90.0);
std::uniform_real_distribution<> lon_dist(-180.0, 180.0);
double lat = lat_dist(gen);
double lon = lon_dist(gen);
return std::to_string(lat) + "," + std::to_string(lon);
} else { // 10% chance of invalid coordinates
std::uniform_int_distribution<> error_dist(0, 2);
int error_type = error_dist(gen);
if (error_type == 0) {
return "abc,def"; // Completely invalid format
} else if (error_type == 1) {
std::uniform_real_distribution<> lon_dist(-180.0, 180.0);
double lon = lon_dist(gen);
return "100.0," + std::to_string(lon); // Invalid latitude (100 > 90)
} else {
std::uniform_real_distribution<> lat_dist(-90.0, 90.0);
double lat = lat_dist(gen);
return std::to_string(lat) + ",200.0"; // Invalid longitude (200 > 180)
}
}
}
struct Coordinate {
double latitude;
double longitude;
};
struct InvalidDoubleConversion { std::string_view message; };
struct InvalidCoordinate { std::string_view message; };
struct InvalidCoordinateFormat { std::string_view message; };
std::expected<double, InvalidDoubleConversion> safe_str_to_double_expected(std::string_view str) {
double value = 0.0;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
if (ec == std::errc::invalid_argument) {
return std::unexpected{InvalidDoubleConversion{"Invalid number format"}};
}
if (ec == std::errc::result_out_of_range) {
return std::unexpected{InvalidDoubleConversion{"Number out of range"}};
}
if (ptr != str.data() + str.size()) {
return std::unexpected{InvalidDoubleConversion{"Extra characters found in number"}};
}
return value;
}
std::expected<Coordinate, std::variant<InvalidDoubleConversion, InvalidCoordinate, InvalidCoordinateFormat>>
parse_coordinate_expected(const std::string& input) {
std::istringstream ss(input);
std::string lat_str, lon_str;
if (!std::getline(ss, lat_str, ',') || !std::getline(ss, lon_str)) {
return std::unexpected{InvalidCoordinateFormat{"Invalid format (expected 'latitude,longitude')"}};
}
auto lat_result = safe_str_to_double_expected(lat_str);
if (!lat_result.has_value()) {
return std::unexpected{lat_result.error()};
}
auto lon_result = safe_str_to_double_expected(lon_str);
if (!lon_result.has_value()) {
return std::unexpected{lon_result.error()};
}
double latitude = lat_result.value();
double longitude = lon_result.value();
if (latitude < -90 || latitude > 90) {
return std::unexpected{InvalidCoordinate{"Latitude out of range (-90 to 90)"}};
}
if (longitude < -180 || longitude > 180) {
return std::unexpected{InvalidCoordinate{"Longitude out of range (-180 to 180)"}};
}
return Coordinate{latitude, longitude};
}
Result<double, Error<InvalidCoordinate, InvalidDoubleConversion >> safe_str_to_double_cppmatch(std::string_view str) {
double value = 0.0;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
if (ec == std::errc::invalid_argument)
return InvalidDoubleConversion{"Invalid number format"};
if (ec == std::errc::result_out_of_range)
return InvalidDoubleConversion{"Number out of range"};
if (ptr != str.data() + str.size())
return InvalidDoubleConversion{"Extra characters found in number"};
return value;
}
Result<Coordinate, Error<InvalidDoubleConversion, InvalidCoordinate, InvalidCoordinateFormat>>
parse_coordinate_cppmatch(const std::string& input) {
std::istringstream ss(input);
std::string lat_str, lon_str;
if (!std::getline(ss, lat_str, ',') || !std::getline(ss, lon_str))
return InvalidCoordinateFormat{"Invalid format (expected 'latitude,longitude')"};
double latitude = expect(safe_str_to_double_cppmatch(lat_str));
double longitude = expect(safe_str_to_double_cppmatch(lon_str));
if (latitude < -90 || latitude > 90)
return InvalidCoordinate{"Latitude out of range (-90 to 90)"};
if (longitude < -180 || longitude > 180)
return InvalidCoordinate{"Longitude out of range (-180 to 180)"};
return Coordinate{latitude, longitude};
}
Result<Coordinate, Error<InvalidDoubleConversion, InvalidCoordinate, InvalidCoordinateFormat>>
parse_coordinate_cppmatch_with_exceptions(const std::string& input) {
std::istringstream ss(input);
std::string lat_str, lon_str;
if (!std::getline(ss, lat_str, ',') || !std::getline(ss, lon_str))
return InvalidCoordinateFormat{"Invalid format (expected 'latitude,longitude')"};
double latitude = expect_e(safe_str_to_double_cppmatch(lat_str));
double longitude = expect_e(safe_str_to_double_cppmatch(lon_str));
if (latitude < -90 || latitude > 90)
return InvalidCoordinate{"Latitude out of range (-90 to 90)"};
if (longitude < -180 || longitude > 180)
return InvalidCoordinate{"Longitude out of range (-180 to 180)"};
return Coordinate{latitude, longitude};
}
double safe_str_to_double_throws(std::string_view str) {
double value = 0.0;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
if (ec == std::errc::invalid_argument) {
throw InvalidDoubleConversion{"Invalid number format"};
}
if (ec == std::errc::result_out_of_range) {
throw InvalidDoubleConversion{"Number out of range"};
}
if (ptr != str.data() + str.size()) {
throw InvalidDoubleConversion{"Extra characters found in number"};
}
return value;
}
Coordinate parse_coordinate_throws(const std::string& input) {
std::istringstream ss(input);
std::string lat_str, lon_str;
if (!std::getline(ss, lat_str, ',') || !std::getline(ss, lon_str)) {
throw InvalidCoordinateFormat{"Invalid format (expected 'latitude,longitude')"};
}
double latitude = safe_str_to_double_throws(lat_str);
double longitude = safe_str_to_double_throws(lon_str);
if (latitude < -90 || latitude > 90) {
throw InvalidCoordinate{"Latitude out of range (-90 to 90)"};
}
if (longitude < -180 || longitude > 180) {
throw InvalidCoordinate{"Longitude out of range (-180 to 180)"};
}
return Coordinate{latitude, longitude};
}
static void coord_expected(benchmark::State& state) {
for (auto _ : state) {
std::string input = generate_random_coordinate_string();
auto result = parse_coordinate_expected(input);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(coord_expected);
static void coord_cppmatch(benchmark::State& state) {
for (auto _ : state) {
std::string input = generate_random_coordinate_string();
auto result = parse_coordinate_cppmatch(input);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(coord_cppmatch);
static void coord_cppmatch_with_exceptions(benchmark::State& state) {
for (auto _ : state) {
std::string input = generate_random_coordinate_string();
auto result = match_e(
parse_coordinate_cppmatch_with_exceptions(input), [](auto){return "";});
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(coord_cppmatch_with_exceptions);
static void coord_throws(benchmark::State& state) {
for (auto _ : state) {
std::string input = generate_random_coordinate_string();
try {
auto result = parse_coordinate_throws(input);
benchmark::DoNotOptimize(result);
} catch (const InvalidDoubleConversion& e) {
benchmark::DoNotOptimize(e);
} catch (const InvalidCoordinate& e) {
benchmark::DoNotOptimize(e);
} catch (const InvalidCoordinateFormat& e) {
benchmark::DoNotOptimize(e);
}
}
}
BENCHMARK(coord_throws);
BENCHMARK_MAIN();