字符串数值转换,性能对比,批量转换可以考虑使用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().

Function Time (ns) Speedup
sprintf 194.225 1.00x
vc 61.522 3.16x
naive 26.743 7.26x
count 20.552 9.45x
lut 17.810 10.91x
countlut 9.926 19.57x
branchlut 8.430 23.04x
sse2 7.614 25.51x
null 2.230 87.09x

Implementations

Function  Description
ostringstream std::ostringstream in C++ standard library.
ostrstream std::ostrstream in C++ standard library.
to_string std::to_string() in C++11 standard library.
sprintf sprintf() in C standard library
vc Visual C++'s _itoa()_i64toa()_ui64toa()
naive Compute division/modulo of 10 for each digit, store digits in temp array and copy to buffer in reverse order.
unnamed Compute division/modulo of 10 for each digit, store directly in buffer
count Count number of decimal digits first, using technique from [1].
lut Uses lookup table (LUT) of digit pairs for division/modulo of 100. Mentioned in [2]
countlut Combines count and lut.
branchlut Use branching to divide-and-conquer the range of value, make computation more parallel.
sse2 Based 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)
null Do 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 
ostringstream 2,778.748 0.45x
ostrstream 2,628.365 0.48x
gay 1,646.310 0.76x
sprintf 1,256.376 1.00x
fpconv 273.822 4.59x
grisu2 220.251 5.70x
doubleconv 201.645 6.23x
milo 138.021 9.10x
null 2.146 585.58x

Implementations

Function  Description
ostringstream std::ostringstream in C++ standard library with setprecision(17).
ostrstream std::ostrstream in C++ standard library with setprecision(17).
sprintf sprintf() in C standard library with "%.17g" format.
stb_sprintf fast sprintf replacement with "%.17g" format.
gay David M. Gay's dtoa() C implementation.
grisu2 Florian Loitsch's Grisu2 C implementation [1].
doubleconv C++ 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).
fpconv night-shift's Grisu2 C implementation.
milo miloyip's Grisu2 C++ header-only implementation.
null Do 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

性能 - 字符串数值类型转换相关推荐

  1. 注册表中性能计数器说明文字字符串数值的格式不正确

    http://support.microsoft.com/kb/300956/zh-cn http://support.microsoft.com/kb/300956/en-us 注册表中性能计数器说 ...

  2. ES6变量常量字符串数值

    [转]ES6之变量常量字符串数值 ECMAScript 6 是 JavaScript 语言的最新一代标准,当前标准已于 2015 年 6 月正式发布,故又称 ECMAScript 2015. ES6对 ...

  3. Scala数值类型转换

    Scala数值类型转换 自动转换 1.基本说明 2.案例实操 强制类型转换 1. 基本说明 2. 案例实操 数值类型和 String 类型间转换 1.基本说明 2.案例实操 自动转换 1.基本说明 当 ...

  4. 【Windows 逆向】Cheat Engine 数据挖掘搜索方法和技巧 ( 数值类型选择 | 字符串数值类型选择 | 全部数值类型模糊选择 )

    文章目录 一.数值类型选择 二.字符串数值类型选择 三.全部数值类型模糊选择 一.数值类型选择 在 CE 中可以搜索多种数据类型 , 如下图 , 二进制 , 字节 , 2 字节 , 4 字节 , 8 ...

  5. 32位hex转浮点 python_python——int()、hex()、oct()、bin()、float()数值类型转换函数

    摘要:在python中,数值类型转换函数常用的有浮点型float().取整int().八进制oct().二进制bin().十六进制hex()这五个函数. 单词float的意思就是浮动的意思: int是 ...

  6. 字符串数值的比较 java

    字符串数值的比较 视频 java演练 字符串的值比较 ==与equals package bank.com;import java.util.Scanner;public class test {pu ...

  7. Spark(Hive)对字符串数值的排序

    前言 对于字符串数值的排序底层是按照ASCII码规则进行排序的,说的简单点就是在对字符串类型的数字值排序时优先排第一位,然后第一位有相同的比较第二位,多位数以此类推.,因此一定要先将字符串转为Int( ...

  8. qt4.8与达梦数据库间的插入和更新字符串数值问题

    最近使用qt-creator2.4.1和qt-4.8.1-mingw对达梦数据库进行插入和更新字符串数值发现了一些小问题,也许是qt4本身与达梦DM7之间的一些问题,个人不是很清楚,但是也有正确实现的 ...

  9. C++实现 字符串+数值

    前言 在用C++进行编码的时候,有时候需要经常用到字符串+数值,但是C++又不内置这种计算,这个时候就需要我们自己重载运算符+来实现上述功能. 一.C++的运算符重载 C++的运算符重载有两种方式,一 ...

最新文章

  1. 信息化及信息化的五个层次
  2. YARN的内存和CPU配置优化
  3. Joyoshare VidiKit教程:如何将字幕添加到WMV电影中?
  4. SQL server 2008 如何卸载干净
  5. IDEA代码格式化后缩进符不正确
  6. Android:Json数据转换成Map
  7. 小程序 加快安卓手机向蓝牙设备发送大数据
  8. 异步加载loading
  9. 【AC.HASH】OpenHarmony啃论文俱乐部——在基于位置的隐私感知服务中实现K-匿名之浅析
  10. c语言 结构体 ppt,第8章C语言的结构体和共同体.ppt
  11. ios 去除字符串首尾空格、换行
  12. java八大数据类型详解及其变量详解
  13. 首款国产7纳米GPGPU芯片在上海问世
  14. 史上最全最详细多种手机主流操作系统详解
  15. 在Servlet之前的CGI是个什么东西
  16. 扰码器(三)并行扰码器综述及设计思路
  17. 【STM32标准库】【基础知识】外部中断
  18. CS5265参数说明|CS5265设计资料|CS5265设计电流|type-CtoHDMI2.0拓展坞资料
  19. WPS Excel做多级下拉菜单列表
  20. c语言程序设计答案第6章,C语言程序设计答案(黄保和编)第6章

热门文章

  1. 老贴不过觉得还是有意思:中国男足为什么总是输?
  2. 使用鼠标从Matplotlib显示的图像中取点,画框
  3. python基础教程:PyCharm第一次安装及使用教程
  4. 6300v2 php服务器,网件R6300 V2与R6300 V1的区别
  5. OLED上播放动图的一种笨方法
  6. 高科路由器有虚拟服务器设置吗,高科路由器怎么设置 -电脑资料
  7. 使用正则表达式匹配全角空格
  8. 计算机cct考试在线答题,计算机一级(CCT)练习题及答案
  9. 使用Web Scraper插件实现简单爬虫
  10. C# winform 编写一键排班软件时遇到的问题