C ++最有效的将字符串转换为int的方法(比atoi快) [英] C++ most efficient way to convert string to int (faster than atoi)
问题描述
如标题中提到的,我正在寻找能够给我比atoi更多性能的东西。目前,我知道的最快的方法是
As mentioned in the title, I'm looking for something that can give me more performance than atoi. Presently, the fastest way I know is
atoi(mystring.c_str())
最后,我更喜欢不依赖Boost的解决方案。是否有任何人有这样的性能技巧这样做?
Finally, I would prefer a solution that doesn't rely on Boost. Does anybody have good performance tricks for doing this?
其他信息:int不会超过20亿,它总是正的,字符串中没有小数位。 / p>
Additional Information: int will not exceed 2 billion, it is always positive, the string has no decimal places in it.
推荐答案
atoi
这在Andrei Alexandrescu在C ++和2012年之后的会议上的演讲中得到了强有力的证明。 Hi s替换使用循环展开和ALU并行性,以实现数量级的整体改进。我没有他的资料,但此链接使用类似的技术: http://tombarta.wordpress .com / 2008/04/23 / specializing-atoi /
atoi
can be improved upon significantly, given certain assumptions. This was demonstrated powerfully in a presentation by Andrei Alexandrescu at the C++ and Beyond 2012 conference. Hi s replacement used loop unrolling and ALU parallelism to achieve orders of magnitude in perf improvement. I don't have his materials, but this link uses a similar technique: http://tombarta.wordpress.com/2008/04/23/specializing-atoi/
这篇关于C ++最有效的将字符串转换为int的方法(比atoi快)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!