From 6d92131fc7a1195eef8ae1dc7a3d9fb448a8d0f3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 5 Apr 2023 13:59:05 +0200 Subject: [PATCH] Add a benchmark for wxString::FromDouble() and FromCDouble() too Also compare them with snprintf("%d") and std::to_chars, which is again significantly faster. --- tests/benchmarks/strings.cpp | 90 ++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/benchmarks/strings.cpp b/tests/benchmarks/strings.cpp index c606a8813f..7c03ff7688 100644 --- a/tests/benchmarks/strings.cpp +++ b/tests/benchmarks/strings.cpp @@ -551,6 +551,21 @@ const struct ToDoubleData { "-3E-abcde5", 0, false }, }; +const struct FromDoubleData +{ + double value; + int prec; + const char *str; +} fromDoubleData[] = +{ + { 1.23, -1, "1.23" }, + { -0.45678, -1, "-0.45678" }, + { 1.2345678, 0, "1" }, + { 1.2345678, 1, "1.2" }, + { 1.2345678, 2, "1.23" }, + { 1.2345678, 3, "1.235" }, +}; + } // anonymous namespace BENCHMARK_FUNC(StringToDouble) @@ -583,6 +598,30 @@ BENCHMARK_FUNC(StringToCDouble) return true; } +BENCHMARK_FUNC(StringFromDouble) +{ + for ( const auto& data : fromDoubleData ) + { + const wxString& s = wxString::FromDouble(data.value, data.prec); + if ( wxStrcmp(s.utf8_str(), data.str) != 0 ) + return false; + } + + return true; +} + +BENCHMARK_FUNC(StringFromCDouble) +{ + for ( const auto& data : fromDoubleData ) + { + const wxString& s = wxString::FromCDouble(data.value, data.prec); + if ( wxStrcmp(s.utf8_str(), data.str) != 0 ) + return false; + } + + return true; +} + BENCHMARK_FUNC(Strtod) { double d = 0.; @@ -601,6 +640,29 @@ BENCHMARK_FUNC(Strtod) return true; } +BENCHMARK_FUNC(PrintfDouble) +{ + char buf[64]; + for ( const auto& data : fromDoubleData ) + { + if ( data.prec == -1 ) + { + if ( !snprintf(buf, sizeof(buf), "%g", data.value) ) + return false; + } + else + { + if ( !snprintf(buf, sizeof(buf), "%.*f", data.prec, data.value) ) + return false; + } + + if ( strcmp(buf, data.str) != 0 ) + return false; + } + + return true; +} + #if wxCHECK_CXX_STD(201703L) #include @@ -623,6 +685,34 @@ BENCHMARK_FUNC(StdFromChars) return true; } + +BENCHMARK_FUNC(StdToChars) +{ + char buf[64]; + std::to_chars_result res; + for ( const auto& data : fromDoubleData ) + { + if ( data.prec == -1 ) + { + res = std::to_chars(buf, buf + sizeof(buf), data.value); + } + else + { + res = std::to_chars(buf, buf + sizeof(buf), data.value, + std::chars_format::fixed, data.prec); + } + + if ( res.ec != std::errc{} ) + return false; + + *res.ptr = '\0'; + + if ( strcmp(buf, data.str) != 0 ) + return false; + } + + return true; +} #endif // __cpp_lib_to_chars #endif // C++17