简体   繁体   English

c-将char *数组转换为int和float的最有效方法是什么?

[英]c - What is the most efficient way of converting char * array to int and float?

Sorry if it was already asked before, but I really couldn't find a clear answer to this question. 抱歉,以前是否有人问过,但是我真的找不到这个问题的明确答案。 Please, tell me, what is the fastest way of converting: 请告诉我,最快的转换方法是:

char * array to int
char * array to float

char * array stores a number, for example: char *数组存储一个数字,例如:

"12345"
"1234,56789"

If the fastest ways for x86 and x64 systems are different, please tell both of them. 如果用于x86和x64系统的最快方法不同,请告诉他们两者。

EDIT: By "fastest", I mean "the most efficient", not "the most simple to use". 编辑:“最快”是指“最高效”,而不是“最简单易用”。

Start with atoi() and atof(), measure the speed, decide if it's a problem - then look for a solution 从atoi()和atof()开始,测量速度,确定是否有问题-然后寻找解决方案

edit - it's almost certain that the I/O will dominate any time spent doing the conversion. 编辑- 几乎可以确定,I / O将主导转换所花费的所有时间。 If the conversion time does matter (after you profile) then if you know the format ie. 如果转换时间确实很重要(在配置文件之后),那么您是否知道格式即。 ints are always 4digits, floats always have 5 decimal places, you might be able to write a hand tuned routine that does better than the standard lib 整数始终为4位,浮点数始终为5个小数位,您也许可以编写比标准lib更好的手动优化例程

You can just use 你可以用

atoi()

for ints and 用于整数和

atof()

for floats. 用于花车。

For int , the obvious loop is probably the fastest. 对于int ,明显的循环可能是最快的。

For float , it's much more complicated. 对于float ,它要复杂得多。 If you don't care if the result is right, a similarly trivial loop is also fine. 如果您不在乎结果是否正确,类似的琐碎循环也可以。 If you need the exact result, you'll want to use strtod or atof or similar since rolling your own is extremely difficult. 如果您需要精确的结果,则将需要使用strtodatof或类似方法,因为自己滚动非常困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM