概述
說明
Boost.Charconv 可將字元緩衝區轉換為數字,以及將數字轉換為字元緩衝區。它是一個小型函式庫,包含兩個多載函式來執行繁重的工作,以及數個支援的列舉、結構、模板和常數,特別注重效能以及跨支援開發環境的一致性。
為什麼我應該對這個函式庫感興趣?Charconv 與地區設定無關、非配置1、非擲回,且只需要最低 C++ 11 標準。它提供類似於 std::printf
或 std::strtod
的功能,並具有顯著的效能提升。如果您的工具鏈不支援標準函式庫 <charconv>
,也可以使用此函式庫來代替。目前只有 GCC 11+ 和 MSVC 19.24+ 在其實作的 <charconv>
中支援整數和浮點數轉換。
如果您正在使用這些編譯器,Boost.Charconv 的效能至少與 <charconv>
相當,並且速度甚至可以快上數倍。請參閱:效能測試
1 可能發生配置的唯一例外情況是,您正在將字串解析為 80 或 128 位元的 long double
或 __float128
,且字串長度超過 1024 個位元組。
支援的編譯器/作業系統
Boost.Charconv 已經在 Ubuntu、macOS 和 Windows 上使用下列編譯器進行測試
-
GCC 5 或更新版本
-
Clang 3.8 或更新版本
-
Visual Studio 2015 (14.0) 或更新版本
已在 GitHub Actions 和 Drone 上測試。
入門指南
B2
執行下列命令以複製最新版本的 Boost 和 Charconv,準備 Boost.Build 系統以供使用,並以 C++11 作為預設標準建置函式庫
git clone https://github.com/boostorg/boost
cd boost
git submodule update --init
cd ..
./bootstrap
./b2 cxxstd=11
要安裝開發環境,請執行
sudo ./b2 install cxxstd=11
cxxstd 的值必須至少為 11。有關所有有效值,請參閱b2 文件中的 cxxstd
部分。
__float128
和 std::float128_t
支援
如果使用 B2 或 CMake,建置系統會自動定義 BOOST_CHARCONV_HAS_QUADMATH
並連結到它,前提是建置系統可以成功執行一個小型測試案例。如果您使用其他建置系統,並且需要支援這些類型,則必須定義 BOOST_CHARCONV_HAS_QUADMATH
,並連結到 libquadmath。
重要
|
libquadmath 僅在受支援的平台上可用(例如,搭載 x86、x86_64、PPC64 和 IA64 的 Linux)。 |
相依性
此函式庫依賴於:Boost.Assert、Boost.Config、Boost.Core,以及可選的 libquadmath(見上文)。
基本使用範例
使用範例
#include <boost/charconv.hpp>
const char* buffer = "42";
int v = 0;
boost::charconv::from_chars_result r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc());
assert(v == 42);
char buffer[64];
int v = 123456;
boost::charconv::to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), v);
assert(r.ec == std::errc());
assert(!strncmp(buffer, "123456", 6)); // Strncmp returns 0 on match
API 參考
from_chars
from_chars 概述
from_chars
是一組函式,它會嘗試根據指定的 chars_format
(如果適用)將 [first, last)
範圍內的字串轉換為 value
。數字的解析與語系無關(例如,等同於「C」語系)。from_chars
的結果是 from_chars_result
,成功時返回 ptr == last
且 ec == std::errc()
,失敗時返回 ptr
等於最後一個有效解析的字元,或者在值下溢/上溢時返回 last
,且 ec == std::errc::invalid_argument
或 std::errc::result_out_of_range
。from_chars
不需要字元序列以 null 終止。
定義
namespace boost { namespace charconv {
struct from_chars_result
{
const char* ptr;
std::errc ec;
friend constexpr bool operator==(const from_chars_result& lhs, const from_chars_result& rhs) noexcept = default;
constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
}
template <typename Integral>
BOOST_CXX14_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, Integral& value, int base = 10) noexcept;
template <typename Integral>
BOOST_CXX14_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, Integral& value, int base = 10) noexcept;
BOOST_CXX14_CONSTEXPR from_chars_result from_chars<bool>(const char* first, const char* last, bool& value, int base) = delete;
template <typename Real>
from_chars_result from_chars(const char* first, const char* last, Real& value, chars_format fmt = chars_format::general) noexcept;
template <typename Real>
from_chars_result from_chars(boost::core::string_view sv, Real& value, chars_format fmt = chars_format::general) noexcept;
// See note below Usage notes for from_chars for floating point types
template <typename Real>
from_chars_result from_chars_erange(const char* first, const char* last, Real& value, chars_format fmt = chars_format::general) noexcept;
template <typename Real>
from_chars_result from_chars_erange(boost::core::string_view sv, Real& value, chars_format fmt = chars_format::general) noexcept;
}} // Namespace boost::charconv
from_chars 參數
-
first
,last
- 指向要解析的有效範圍的指標 -
sv
- 要解析的有效範圍的字串檢視。與 boost::core::string_view、std::string 和 std::string_view 相容 -
value
- 成功解析後儲存輸出的位置 -
base
(僅限整數)- 要使用的整數基底。必須介於 2 到 36 之間(含) -
fmt
(僅限浮點數)- 緩衝區的格式。有關說明,請參閱chars_format 概觀。
from_chars_result
-
ptr
- 從from_chars
返回後,它是一個指向與模式不符的第一個字元的指標,如果所有字元都成功解析,則指向last
。 -
ec
- 錯誤碼。from_chars
返回的值為
傳回值 |
說明 |
|
成功解析 |
|
1) 將負數解析為無號類型 2) 前導 3) 前導空格 4) 不相容的格式(例如, |
|
1) 上溢 2) 下溢 |
-
operator==
- 比較 ptr 和 ec 的值是否相等
使用說明
整數類型 from_chars 的使用說明
-
允許所有內建整數類型,除了 bool(已刪除)
-
這些函式已經過測試,支援
__int128
和unsigned __int128
-
使用
-std=c++14
或更新版本編譯時,整數類型的 from_chars 是 constexpr。-
一個已知的例外是 GCC 5,它不支援
const char*
的 constexpr 比較。
-
-
有效的字串只能包含數字字元。前導空格不會被忽略,並且會返回
std::errc::invalid_argument
。
浮點數類型 from_chars 的使用說明
-
在
std::errc::result_out_of_range
上,我們針對小值(例如 1.0e-99999)返回 ±0,針對大值(例如 1.0e+99999)返回 ±HUGE_VAL,以符合std::strtod
的處理方式。這與標準有所不同,標準規定我們應該返回未修改的value
參數。-
from_chars
在 LWG 有一個未解決的問題:https://cplusplus.github.io/LWG/lwg-active.html#3081。<charconv> 的標準不像 strtod 那樣區分下溢和上溢。假設您正在編寫一個 JSON 函式庫,並且為了效能因素,您將std::strtod
替換為boost::charconv::from_chars
。Charconv 在某些轉換時會返回 std::errc::result_out_of_range。然後您必須自己再次解析字串,才能找出導致std::errc::result_out_of_range
的四種可能原因中的哪一種。Charconv 可以透過在整個程式碼庫中使用boost::charconv::from_chars_erange
取代boost::charconv::from_chars
來提供您該資訊。透過實作符合既定 strtod 行為的 LWG 問題解決方案,我認為我們在不等待委員會決定的情況下提供了正確的行為。
-
-
這些函式已經過測試,支援所有內建浮點數類型以及 C++23 的
<stdfloat>
中的類型。-
長雙精度浮點數 (long double) 可以是 64、80 或 128 位元,但必須符合 IEEE 754 標準。一個不符合標準,因此不受支援的格式範例是
__ibm128
。 -
使用
__float128
或std::float128_t
需要使用-std=gnu++xx
編譯並連結 GCC 的libquadmath
。使用 CMake 建置時,這會自動完成。
-
範例
基本用法
整數
const char* buffer = "42";
int v = 0;
from_chars_result r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 42);
std::string str_buffer (buffer);
boost::core::string_view sv(str_buffer);
int v2;
auto r2 = boost::charconv::from_chars(sv, v2);
assert(r);
assert(v2 == v);
浮點數
const char* buffer = "1.2345"
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 1.2345);
std::string str_buffer(buffer);
double v2;
auto r2 = boost::charconv::from_chars(buffer, v2);
assert(r2);
assert(v == v2);
十六進位
整數
const char* buffer = "2a";
unsigned v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, 16);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 42);
浮點數
const char* buffer = "1.3a2bp-10";
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, boost::charconv::chars_format::hex);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 8.0427e-18);
std::errc::invalid_argument
以下程式碼無效,因為正在將負值解析為無符號整數。
const char* buffer = "-123";
unsigned v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc::invalid_argument);
assert(!r); // Same as above but less verbose. Added in C++26.
以下程式碼無效,因為固定格式的浮點數值不能有指數。
const char* buffer = "-1.573e-3";
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, boost::charconv::chars_format::fixed);
assert(r.ec == std::errc::invalid_argument);
assert(!r); // Same as above but less verbose. Added in C++26.
注意:如果發生 std::errc::invalid_argument
,from_chars
不會修改 v。
std::errc::result_out_of_range
const char* buffer = "1234";
unsigned char v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc::result_out_of_range);
assert(!r); // Same as above but less verbose. Added in C++26.
assert(v == 0)
注意:如果發生 std::errc::result_out_of_range
,from_chars
不會修改 v。
to_chars
to_chars 概述
to_chars
是一組函式,嘗試將 value
轉換為由 [first, last)
指定的字元緩衝區。to_chars
的結果是 to_chars_result
,成功時返回的 ptr
等於寫入字元的結尾位置,且 ec == std::errc()
;失敗時返回 std::errc::value_too_large
,且 ptr == last
。to_chars
不會在返回的字元後加上 null 終止符。
定義
namespace boost { namespace charconv {
struct to_chars_result
{
char* ptr;
std::errc ec;
friend constexpr bool operator==(const to_chars_result& lhs, const to_chars_result& rhs) noexcept; = default;
constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
};
template <typename Integral>
BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars(char* first, char* last, Integral value, int base = 10) noexcept;
template <typename Integral>
BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars<bool>(char* first, char* last, Integral value, int base) noexcept = delete;
template <typename Real>
to_chars_result to_chars(char* first, char* last, Real value, chars_format fmt = chars_format::general, int precision) noexcept;
}} // Namespace boost::charconv
to_chars 參數
-
first, last
- 指向字元緩衝區開頭和結尾的指標。 -
value
- 要解析到緩衝區的值。 -
base
(僅限整數)- 要使用的整數基底。必須介於 2 到 36 之間(含) -
fmt
(僅限浮點數)- 要使用的浮點數格式。有關說明,請參閱 chars_format 概述。 -
precision
(僅限浮點數)- 所需的小數位數。
to_chars_result
-
ptr
- 從to_chars
返回後,指向成功時寫入字元的結尾位置,或失敗時指向last
。 -
ec
- 錯誤碼。to_chars
返回的值為
傳回值 |
說明 |
|
成功解析 |
|
1) 上溢 2) 下溢 |
-
operator==
- 比較 ptr 和 ec 的值是否相等。
使用說明
整數類型 to_chars 的使用說明
-
允許所有內建整數類型,除了 bool(已刪除)
-
當符合以下條件時,整數類型的 from_chars 是 constexpr(BOOST_CHARCONV_CONSTEXPR 已定義):
-
使用
-std=c++14
或更新版本編譯。 -
使用具有
__builtin_ is_constant_evaluated
的編譯器。
-
-
這些函式已經過測試,支援
__int128
和unsigned __int128
浮點數類型 to_chars 的使用說明
-
處理不同
NaN
值時,將返回以下內容:-
qNaN
返回 "nan" -
-qNaN
返回 "-nan(ind)" -
sNaN
返回 "nan(snan)" -
-sNaN
返回 "-nan(snan)"
-
-
這些函式已經過測試,支援所有內建浮點數類型以及 C++23 的
<stdfloat>
中的類型。-
長雙精度浮點數可以是 64、80 或 128 位元,但必須符合 IEEE 754 標準。一個不符合標準,因此不受支援的格式範例是
ibm128
。 -
使用
__float128
或std::float128_t
需要使用-std=gnu++xx
編譯並連結 GCC 的libquadmath
。使用 CMake 建置時,這會自動完成。
-
範例
基本用法
整數
char buffer[64] {};
int v = 42;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v);
assert(r.ec == std::errc());
assert(!strcmp(buffer, "42")); // strcmp returns 0 on match
浮點數
char buffer[64] {};
double v = 1e300;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "1e+300"));
十六進位
整數
char buffer[64] {};
int v = 42;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v, 16);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "2a")); // strcmp returns 0 on match
浮點數
char buffer_u[64] {};
double u = -1.08260383390082950e+20;
char buffer_v[64] {};
double v = -1.08260383390082946e+20;
to_chars(buffer_u, buffer_u + sizeof(buffer_u) - 1, u, chars_format::hex);
to_chars(buffer_v, buffer_v + sizeof(buffer_v) - 1, v, chars_format::hex);
std::cout << "U: " << buffer_u << "\nV: " << buffer_v << std::endl;
// U: -1.779a8946bb5fap+66
// V: -1.779a8946bb5f9p+66
//
// With hexfloats we can see the ULP distance between U and V is a - 9 == 1.
std::errc::value_too_large
整數
char buffer[3] {};
int v = -1234;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v, 16);
assert(r.ec == std::errc::value_too_large);
assert(!r); // Same as above but less verbose. Added in C++26.
浮點數
char buffer[3] {};
double v = 1.2345;
auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v);
assert(r.ec == std::errc::value_too_large);
assert(!r); // Same as above but less verbose. Added in C++26.
如果發生 std::errc::value_too_large
,to_chars_result.ptr 等於 last
。
chars_format
chars_format 概述
boost::charconv::chars_format
是一個 enum class
,用於定義 from_chars
和 to_chars
的浮點數類型格式。
定義
namespace boost { namespace charconv {
enum class chars_format : unsigned
{
scientific = 1 << 0,
fixed = 1 << 1,
hex = 1 << 2,
general = fixed | scientific
};
}} // Namespace boost::charconv
格式
科學記號格式
科學計數法格式為 1.3e+03
。整數部分將介於 0 到 9 之間(含)。小數部分和指數部分將始終出現。指數部分將始終至少有 2 位數字。
定點數格式
固定格式將會是 2.30
或 3090
的形式。此格式不會出現指數。如果 to_chars
的精度超過類型的精度(例如 std::numeric_limits<double>::chars10
),則會在有效數字的末尾附加 0。
十六進位格式
十六進位格式將會是 1.0cp+05
的形式。整數部分始終為 0 或 1。指數將使用 p 而不是像以 10 為基底的格式那樣使用 e,因為 e 是有效的十六進位值。**注意:每個二進位浮點數都有唯一的十六進位浮點數表示法,但並非每個十六進位浮點數都有唯一的二進位浮點數表示法。**這是因為 IEEE754 binary32 和 binary64 的有效數字位數不能被 4 整除。
十六進位浮點數使用案例
對於不熟悉十六進位浮點數的人來說,它們在特定情況下很有價值
-
精度控制:十六進位浮點數可以更精細地控制浮點數值的精度。在十六進位表示法中,每個數字代表四個位元(一個十六進位數),允許您通過指定一定數量的十六進位數字來直接操作數字的精度。當您需要精確控制計算所需的準確度時,這會很有用。
-
位元層級表示:十六進位浮點數提供浮點數底層位元的直接表示。每個十六進位數字對應於特定的位元組,使其更容易視覺化和理解浮點數值的內部結構。這有助於除錯或分析浮點數算術運算(例如,計算 ULP(最後一位的單位) 距離)。
一般
一般格式將會是數字以固定或一般格式的最短表示法(例如 1234
而不是 1.234e+03
)。
限制
限制概述
<boost/charconv/limits.hpp>
的內容旨在幫助使用者最佳化 to_chars
所需的緩衝區大小。
定義
namespace boost { namespace charconv {
template <typename T>
constexpr int limits<T>::max_chars10;
template <typename T>
constexpr int limits<T>::max_chars;
}} // Namespace boost::charconv
max_chars10
當未傳遞任何基底或傳遞基底 10 時,需要傳遞給 to_chars
以保證所有 T 類型值成功轉換的最小緩衝區大小。
max_chars
對於任何基底值,需要傳遞給 to_chars
以保證所有 T 類型值成功轉換的最小緩衝區大小。
範例
以下兩個範例分別說明了如何使用 max_chars10
來最佳化整數類型和浮點數類型的 to_chars
緩衝區大小。
char buffer [boost::charconv::limits<std::int32_t>::max_chars10;
auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), std::numeric_limits<std::int32_t>::max());
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "2147483647")); // strcmp returns 0 on match
char buffer [boost::charconv::limits<float>::max_chars10;
auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), std::numeric_limits<float>::max());
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "3.40282347e+38")); // strcmp returns 0 on match
以下範例說明了如何使用 max_chars
將整數序列化為二進位(基底 = 2)。
char buffer [boost::charconv::limits<std::uint16_t>::max_chars;
auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), std::numeric_limits<std::uint16_t>::max(), 2);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "1111111111111111")); // strcmp returns 0 on match
效能測試
本節描述了一系列已執行的效能基準測試,比較了此函式庫與標準函式庫,以及如何在需要時執行您自己的基準測試。
這些值是相對於 std::printf
和 std::strtoX
的效能。數字越大,效能越高(例如 2.00 表示速度快兩倍,0.50 表示所需時間是兩倍)。std::printf
和 std::strtoX
始終列在最前面,因為它們將作為參考值。
如何執行效能測試
要自行執行基準測試,請導覽至測試資料夾並在執行測試時定義 BOOST_CHARCONV_RUN_BENCHMARKS
。在 Linux 上使用 b2 的範例:../../../b2 cxxstd=20 toolset=gcc-13 define=BOOST_CHARCONV_RUN_BENCHMARKS STL_benchmark linkflags="-lfmt" -a release
。
此外,您還需要以下內容
-
完全支援
<charconv>
的編譯器-
GCC 11 或更新版本
-
MSVC 19.24 或更新版本
-
結果
x86_64 Linux
表 1 - 4 中的數據是在 Ubuntu 23.04 上使用 x86_64 架構,使用 GCC 13.1.0 和 libstdc++ 執行的。
浮點數
函式 | 相對效能 (float / double) |
---|---|
std::printf |
1.00 / 1.00 |
Boost.lexical_cast |
0.56 / 0.49 |
Boost.spirit.karma |
1.70 / 2.62 |
std::to_chars |
4.01 / 6.03 |
Boost.Charconv.to_chars |
4.46 / 6.20 |
Google double-conversion |
1.26 / 1.91 |
{fmt} |
2.52 / 3.63 |
函式 | 相對效能 (float / double) |
---|---|
std::strto(f/d) |
1.00 / 1.00 |
Boost.lexical_cast |
0.33 / 0.42 |
Boost.spirit.qi |
3.17 / 4.65 |
std::from_chars |
3.23 / 5.77 |
Boost.Charconv.from_chars |
3.28 / 5.75 |
Google double-conversion |
1.16 / 1.30 |
整數
函式 | 相對效能 (uint32_t / uint64_t) |
---|---|
std::printf |
1.00 / 1.00 |
Boost.lexical_cast |
1.80 / 1.38 |
Boost.spirit.karma |
2.81 / 1.62 |
std::to_chars |
4.06 / 2.45 |
Boost.Charconv.to_chars |
4.13 / 2.48 |
{fmt} |
2.88 / 2.21 |
函式 | 相對效能 (uint32_t / uint64_t) |
---|---|
std::strto(ul,ull) |
1.00 / 1.00 |
Boost.lexical_cast |
0.53 / 0.52 |
Boost.spirit.qi |
2.24 / 1.49 |
std::from_chars |
1.97 / 1.68 |
Boost.Charconv.from_chars |
2.54 / 1.78 |
x86_64 Windows
表 5 - 8 的數據是在 Windows 11 x86_64 架構上使用 MSVC 14.3 (V17.7.0) 執行的。
浮點數
函式 | 相對效能 (float / double) |
---|---|
std::printf |
1.00 / 1.00 |
Boost.lexical_cast |
0.50 / 0.70 |
Boost.spirit.karma |
2.23 / 7.58 |
std::to_chars |
5.58 / 15.77 |
Boost.Charconv.to_chars |
5.62 / 15.26 |
函式 | 相對效能 (float / double) |
---|---|
std::strto(f/d) |
1.00 / 1.00 |
Boost.lexical_cast |
0.14 / 0.20 |
Boost.spirit.qi |
2.03 / 4.58 |
std::from_chars |
1.01 / 1.23 |
Boost.Charconv.from_chars |
2.06 / 5.21 |
整數
函式 | 相對效能 (uint32_t / uint64_t) |
---|---|
std::printf |
1.00 / 1.00 |
Boost.lexical_cast |
0.68 / 0.68 |
Boost.spirit.karma |
2.75 / 1.67 |
std::to_chars |
2.75 / 2.10 |
Boost.Charconv.to_chars |
2.75 / 2.06 |
函式 | 相對效能 (uint32_t / uint64_t) |
---|---|
std::strto(ul,ull) |
1.00 / 1.00 |
Boost.lexical_cast |
0.46 / 0.39 |
Boost.spirit.qi |
1.94 / 1.63 |
std::from_chars |
2.43 / 2.18 |
Boost.Charconv.from_chars |
2.68 / 2.27 |
ARM MacOS
表 9-12 的數據是在 macOS Ventura 13.5.2 的 M1 Pro 架構上使用 Homebrew GCC 13.2.0 與 libstdc++ 執行的。
浮點數
函式 | 相對效能 (float / double) |
---|---|
std::printf |
1.00 / 1.00 |
Boost.lexical_cast |
0.58 / 0.16 |
Boost.spirit.karma |
1.39 / 1.22 |
std::to_chars |
6.78 / 6.47 |
Boost.Charconv.to_chars |
7.25 / 6.86 |
Google double-conversion |
2.26 / 2.16 |
{fmt} |
3.78 / 3.38 |
函式 | 相對效能 (float / double) |
---|---|
std::strto(f/d) |
1.00 / 1.00 |
Boost.lexical_cast |
0.06 / 0.06 |
Boost.spirit.qi |
1.12 / 1.06 |
std::from_chars |
1.32 / 1.65 |
Boost.Charconv.from_chars |
1.28 / 1.63 |
Google double-conversion |
0.45 / 0.32 |
整數
函式 | 相對效能 (uint32_t / uint64_t) |
---|---|
std::printf |
1.00 / 1.00 |
Boost.lexical_cast |
2.08 / 1.75 |
Boost.spirit.karma |
4.17 / 2.06 |
std::to_chars |
6.25 / 4.12 |
Boost.Charconv.to_chars |
6.25 / 4.12 |
{fmt} |
5.29 / 3.47 |
函式 | 相對效能 (uint32_t / uint64_t) |
---|---|
std::strto(ul,ull) |
1.00 / 1.00 |
Boost.lexical_cast |
0.56 / 0.54 |
Boost.spirit.qi |
1.39 / 1.33 |
std::from_chars |
1.92 / 1.65 |
Boost.Charconv.from_chars |
2.27 / 1.65 |
來源
以下論文和部落格文章是本函式庫所使用演算法的基礎
-
J.R. Parker 一種通用的字元到整數的轉換方法,軟體:實踐與經驗 15 (8), 1985.
-
Junekey Jeon, 更快的整數格式化 - James Anhalt (jeaiii) 的演算法
-
Junekey Jeon, Dragonbox:一種新的浮點數二進位到十進位轉換演算法
-
Junekey Jeon, 浮點數的固定精度格式化
-
William D. Clinger, 如何精確地讀取浮點數, 1990
-
Daniel Lemire, 每秒千兆位元組的數字解析,軟體:實踐與經驗 51 (8), 2021.
-
Noble Mushtak, Daniel Lemire, 無回退的快速數字解析,軟體:實踐與經驗(即將出版)
-
Ulf Adams, 重新審視 Ryū:printf 浮點數轉換,ACM 程式語言會議記錄第 3 卷,2019
致謝
特別感謝以下人士(非完整名單)
-
感謝 Peter Dimov 提供技術指導並在整個開發過程中為函式庫做出貢獻。
-
感謝 Junekey Jeon 開發並回答我關於他的整數格式化、Dragonbox 和 Floff 的問題。
-
感謝 Chris Kormanyos 擔任函式庫審核經理。
-
感謝 Stephan T. Lavavej 提供基準測試的基礎。
-
感謝所有審核函式庫並提供意見回饋使其變得更好的人。
版權與授權
本文件版權所有 2022-2023 Peter Dimov 和 Matt Borland,並根據 Boost 軟體授權條款 1.0 版 發佈。