From f2b129c5c4a2a7151d4f2684e039ee8a5a3d9217 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Thu, 18 May 2023 16:34:09 +0200 Subject: [PATCH] Add boost.spirit.karma [ci skip] --- test/STL_benchmark.cpp | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/test/STL_benchmark.cpp b/test/STL_benchmark.cpp index 98ec8ec47..67d89b81c 100644 --- a/test/STL_benchmark.cpp +++ b/test/STL_benchmark.cpp @@ -26,7 +26,10 @@ #include #include #include +#include +#include +#include #include #include @@ -50,6 +53,28 @@ constexpr size_t BufSize = 2'000; // more than enough unsigned int global_dummy = 0; +template +void test_boost_spirit_karma(const char* const str, const vector& vec) { + namespace karma = boost::spirit::karma; + + const auto start = steady_clock::now(); + for (size_t k = 0; k < K; ++k) { + for (const auto& elem : vec) { + char buffer[BufSize]; + char* it = buffer; + + if constexpr (std::is_same_v) { + karma::generate(it, karma::float_, elem); + } else { + karma::generate(it, karma::double_, elem); + } + } + } + const auto finish = steady_clock::now(); + + printf("%6.1f ns | %s\n", duration{finish - start}.count() / (N * K), str); +} + template void test_lexical_cast(const char* const str, const vector& vec) { const auto start = steady_clock::now(); @@ -176,7 +201,7 @@ void test_STL_to_chars(const char* const str, const vector& vec, const template void test_boost_to_chars(const char* const str, const vector& vec, const Args&... args) { - char buf[BufSize]; + char buf[BufSize] {}; const auto start = steady_clock::now(); for (size_t k = 0; k < K; ++k) { @@ -206,11 +231,12 @@ void test_boost_to_chars(const char* const str, const vector& vec, con const auto from_result = boost::charconv::from_chars(buf, result.ptr, round_trip, boost_chars_format_from_RoundTrip(RT)); if (from_result.ec != errc() || from_result.ptr != result.ptr || round_trip != elem) { - std::cerr << "Roundtrip failure with: " << elem + std::cerr << std::setprecision(std::numeric_limits::digits10 + 1) + << "Roundtrip failure with: " << elem << "\n to_chars val: " << buf << "\n from_chars val: " << round_trip - << "\n from_chars ptr: " << from_result.ptr - << "\n to_chars ptr: " << to_chars.ptr << std::endl; + << "\n from_chars ptr: " << static_cast(from_result.ptr - buf) + << "\n to_chars ptr: " << static_cast(result.ptr - buf) << std::endl; } } } @@ -375,8 +401,11 @@ void test_all() { } } - test_lexical_cast("Boost float", vec_flt); - test_lexical_cast("Boost double", vec_dbl); + test_lexical_cast("Boost.lexical_cast float", vec_flt); + test_lexical_cast("Boost.lexical_cast double", vec_dbl); + + test_boost_spirit_karma("Boost.spirit.karma float", vec_flt); + test_boost_spirit_karma("Boost.spirit.karma double", vec_dbl); test_sprintf("CRT float scientific 8", vec_flt, "%.8e"); test_sprintf("CRT double scientific 16", vec_dbl, "%.16e");