性能 - 字符串数值类型转换

字符串数值转换,性能对比,批量转换可以考虑使用sscanf

sscanf

sscanf使用demo:

split+atof 和sscanf性能对比

其他函数性能对比

The following are sequential results measured on a PC (Core i7 920 @2.67Ghz), where u32toa() is compiled by Visual C++ 2013 and run on Windows 64-bit. The speedup is based on sprintf().

FunctionTime (ns)Speedup
sprintf194.2251.00x
vc61.5223.16x
naive26.7437.26x
count20.5529.45x
lut17.81010.91x
countlut9.92619.57x
branchlut8.43023.04x
sse27.61425.51x
null2.23087.09x

Implementations

Function Description
ostringstreamstd::ostringstream in C++ standard library.
ostrstreamstd::ostrstream in C++ standard library.
to_stringstd::to_string() in C++11 standard library.
sprintfsprintf() in C standard library
vcVisual C++'s _itoa()_i64toa()_ui64toa()
naiveCompute division/modulo of 10 for each digit, store digits in temp array and copy to buffer in reverse order.
unnamedCompute division/modulo of 10 for each digit, store directly in buffer
countCount number of decimal digits first, using technique from [1].
lutUses lookup table (LUT) of digit pairs for division/modulo of 100. Mentioned in [2]
countlutCombines count and lut.
branchlutUse branching to divide-and-conquer the range of value, make computation more parallel.
sse2Based on branchlut scheme, use SSE2 SIMD instructions to convert 8 digits in parallel. The algorithm is designed by Wojciech Muła [3]. (Experiment shows it is useful for values equal to or more than 9 digits)
nullDo nothing.

Results

The following are sequential results measured on a PC (Core i7 920 @2.67Ghz), where u32toa() is compiled by Visual C++ 2013 and run on Windows 64-bit. The speedup is based on sprintf().

Function Time (ns) Speedup 
ostringstream2,778.7480.45x
ostrstream2,628.3650.48x
gay1,646.3100.76x
sprintf1,256.3761.00x
fpconv273.8224.59x
grisu2220.2515.70x
doubleconv201.6456.23x
milo138.0219.10x
null2.146585.58x

Implementations

Function Description
ostringstreamstd::ostringstream in C++ standard library with setprecision(17).
ostrstreamstd::ostrstream in C++ standard library with setprecision(17).
sprintfsprintf() in C standard library with "%.17g" format.
stb_sprintffast sprintf replacement with "%.17g" format.
gayDavid M. Gay's dtoa() C implementation.
grisu2Florian Loitsch's Grisu2 C implementation [1].
doubleconvC++ implementation extracted from Google's V8 JavaScript Engine with EcmaScriptConverter().ToShortest() (based on Grisu3, fall back to slower bignum algorithm when Grisu3 failed to produce shortest implementation).
fpconvnight-shift's Grisu2 C implementation.
milomiloyip's Grisu2 C++ header-only implementation.
nullDo nothing.

Notes:

  1. tostring() is not tested as it does not fulfill the roundtrip requirement.

  2. Grisu2 is chosen because it can generate better human-readable number and >99.9% of results are in shortest. Grisu3 needs another dtoa() implementation for not meeting the shortest requirement.

参考资料:

https://wiki.so.qihoo.net/pages/viewpage.action?pageId=16145758
https://github.com/compmeist/fast-atof
https://github.com/yuanyuanxiang/_atof
https://github.com/j-jorge/atoi-benchmark

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值