Skip to content

Commit d032208

Browse files
committed
Доработана конвертация в числа. Обновлена версия и результаты бенчмарков.
1 parent 573ffd2 commit d032208

13 files changed

+4445
-4273
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ include(FetchContent)
55

66
project(
77
simstr
8-
VERSION 1.2.5
9-
DESCRIPTION "Yet another string library"
8+
VERSION 1.2.6
9+
DESCRIPTION "Yet another modern C++ string library"
1010
HOMEPAGE_URL "https://github.com/orefkov/simstr"
1111
LANGUAGES CXX
1212
)

bench/bench_str.cpp

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ void ToIntStr0(benchmark::State& state, const std::string& s, int c) {
176176
}
177177

178178
void ToIntFromChars10(benchmark::State& state, const std::string_view& s, int c) {
179-
//#ifdef __EMSCRIPTEN__
180-
//state.SkipWithError("not implemented");
181-
//#else
182179
for (auto _: state) {
183180
int res = 0;
184181
std::from_chars(s.data(), s.data() + s.size(), res, 10);
@@ -190,13 +187,9 @@ void ToIntFromChars10(benchmark::State& state, const std::string_view& s, int c)
190187
#endif
191188
benchmark::DoNotOptimize(res);
192189
}
193-
//#endif
194190
}
195191

196192
void ToIntFromChars16(benchmark::State& state, const std::string_view& s, int c) {
197-
//#ifdef __EMSCRIPTEN__
198-
// state.SkipWithError("not implemented");
199-
//#else
200193
for (auto _: state) {
201194
int res = 0;
202195
std::from_chars(s.data(), s.data() + s.size(), res, 16);
@@ -208,13 +201,12 @@ void ToIntFromChars16(benchmark::State& state, const std::string_view& s, int c)
208201
#endif
209202
benchmark::DoNotOptimize(res);
210203
}
211-
//#endif
212204
}
213205

214206
template<typename T>
215207
void ToIntSimStr10(benchmark::State& state, T t, int c) {
216208
for (auto _: state) {
217-
int res = std::get<0>(t. template to_int<int, true, 10, false, false>());
209+
int res = t. template to_int<int, true, 10, false, false>().value;
218210
#ifdef CHECK_RESULT
219211
if (res != c) {
220212
state.SkipWithError("not equal");
@@ -229,7 +221,7 @@ void ToIntSimStr10(benchmark::State& state, T t, int c) {
229221
template<typename T>
230222
void ToIntSimStr16(benchmark::State& state, T t, int c) {
231223
for (auto _: state) {
232-
int res = std::get<0>(t. template to_int<int, true, 16, false, false>());
224+
int res = t. template to_int<int, true, 16, false, false>().value;
233225
#ifdef CHECK_RESULT
234226
if (res != c) {
235227
state.SkipWithError("not equal");
@@ -244,7 +236,7 @@ void ToIntSimStr16(benchmark::State& state, T t, int c) {
244236
template<typename T>
245237
void ToIntSimStr0(benchmark::State& state, T t, int c) {
246238
for (auto _: state) {
247-
int res = std::get<0>(t. template to_int<int>());
239+
int res = t. template to_int<int>().value;
248240
#ifdef CHECK_RESULT
249241
if (res != c) {
250242
state.SkipWithError("not equal");
@@ -258,7 +250,7 @@ void ToIntSimStr0(benchmark::State& state, T t, int c) {
258250

259251
void ToIntNoOverflow(benchmark::State& state, ssa t, int c) {
260252
for (auto _: state) {
261-
int res = std::get<0>(t.to_int<int, false>());
253+
int res = t.to_int<int, false>().value;
262254
#ifdef CHECK_RESULT
263255
if (res != c) {
264256
state.SkipWithError("not equal");
@@ -287,6 +279,63 @@ BENCHMARK_CAPTURE(ToIntStr0, , std::string{" 123456789"}, 123456789)
287279
BENCHMARK_CAPTURE(ToIntSimStr0, , stringa{" 123456789"}, 123456789) ->Name("stringa s = \" 123456789\"; int res = s.to_int<int>; // Check overflow");
288280
BENCHMARK_CAPTURE(ToIntNoOverflow, , ssa{" 123456789"}, 123456789) ->Name("ssa s = \" 123456789\"; int res = s.to_int<int, false>; // No check overflow");
289281

282+
void ToDoubleStr(benchmark::State& state, const std::string& s, double c) {
283+
for (auto _: state) {
284+
char* ptr = nullptr;
285+
double res = std::strtod(s.c_str(), &ptr);
286+
if (ptr == s.c_str()) {
287+
state.SkipWithError("not equal");
288+
}
289+
#ifdef CHECK_RESULT
290+
if (res != c) {
291+
state.SkipWithError("not equal");
292+
break;
293+
}
294+
#endif
295+
benchmark::DoNotOptimize(res);
296+
}
297+
}
298+
299+
void ToDoubleFromChars(benchmark::State& state, const std::string_view& s, double c) {
300+
for (auto _: state) {
301+
double res = 0;
302+
if (std::from_chars(s.data(), s.data() + s.size(), res).ec != std::errc{}) {
303+
state.SkipWithError("not equal");
304+
}
305+
#ifdef CHECK_RESULT
306+
if (res != c) {
307+
state.SkipWithError("not equal");
308+
break;
309+
}
310+
#endif
311+
benchmark::DoNotOptimize(res);
312+
}
313+
}
314+
315+
template<typename T>
316+
void ToDoubleSimStr(benchmark::State& state, T t, double c) {
317+
for (auto _: state) {
318+
auto r = t.template to_double<false>();
319+
if (!r) {
320+
state.SkipWithError("not equal");
321+
}
322+
double res = *r;
323+
#ifdef CHECK_RESULT
324+
if (res != c) {
325+
state.SkipWithError("not equal");
326+
break;
327+
}
328+
#endif
329+
benchmark::DoNotOptimize(res);
330+
benchmark::DoNotOptimize(t);
331+
}
332+
}
333+
334+
BENCHMARK(__)->Name("----- Convert to double '1234.567e10' ---------")->Repetitions(1);
335+
BENCHMARK_CAPTURE(ToDoubleStr, , std::string{"1234.567e10"}, 1234.567e10) ->Name("std::string s = \"1234.567e10\"; double res = std::strtod(s.c_str(), nullptr);");
336+
BENCHMARK_CAPTURE(ToDoubleFromChars, , std::string_view{"1234.567e10"}, 1234.567e10) ->Name("std::string_view s = \"1234.567e10\"; std::from_chars(s.data(), s.data() + s.size(), res);");
337+
BENCHMARK_CAPTURE(ToDoubleSimStr, , ssa{"1234.567e10"}, 1234.567e10) ->Name("ssa s = \"1234.567e10\"; double res = *s.to_double()");
338+
290339
void AppendStreamConstLiteral(benchmark::State& state) {
291340
for (auto _: state) {
292341
std::string result;

bench/process_result.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ results_vector get_results_infos() {
8484
}
8585
}
8686
}
87-
87+
8888
results_vector results;
89-
89+
9090
if (fileNames.size()) {
9191
std::sort(fileNames.begin(), fileNames.end());
9292
results.reserve(fileNames.size());
@@ -95,7 +95,7 @@ results_vector get_results_infos() {
9595
// В начале имени файла может идти число и дефис, для сортировки, уберём их
9696
// At the beginning of the file name there can be a number and a hyphen, for sorting, remove them
9797
if (auto delimeter = fileName.find('-'); delimeter + 1 > 1) {
98-
if (std::get<1>(fileName(0, delimeter).to_int<unsigned, false, 10, false, false>()) == IntConvertResult::Success) {
98+
if (fileName(0, delimeter).to_int<unsigned, false, 10, false, false>().ec == IntConvertResult::Success) {
9999
fileName.remove_prefix(delimeter + 1);
100100
}
101101
}
@@ -187,7 +187,7 @@ ssa extract_source_for_benchmark(ssa benchName, ssa sourceText) {
187187
static hashStrMapA<stringa> textes;
188188

189189
size_t delim = benchName.find_last('/');
190-
if (delim != str::npos && std::get<1>(benchName(delim + 1).to_int<unsigned, false, 10, false, false>()) == IntConvertResult::Success) {
190+
if (delim != str::npos && benchName(delim + 1).to_int<unsigned, false, 10, false, false>().ec == IntConvertResult::Success) {
191191
benchName.len = delim;
192192
}
193193
auto [it, not_exist] = textes.try_emplace(benchName);
@@ -326,6 +326,9 @@ void write_benchmarks(out_t& out, const results_vector& results, ssa sourceText,
326326
throw std::runtime_error{"Not expected end of file"};
327327
}
328328
line = splitters[idx].next();
329+
if (line.starts_with("std::unordered_map<std::string, size_t> emplace & find std::string_view;_cv")) {
330+
int t = 1;
331+
}
329332
}
330333
}
331334
if (needFooter) {

0 commit comments

Comments
 (0)