任何人都知道如何将巨大的 char 数组转换为 float,非常巨大的数组,性能比 atof/strtod/sscanf 更好
我得到了一个 char 数组,一个巨大的数组 char p[n] 从 txt 中读取。
//1.txt 194.919 -241.808 234.896 195.569 -246.179 234.482 194.919 -241.808 234.896 ...
foo(char *p, 浮点数 x, 浮点数 y, 浮点数 z) 我
尝试
使用atof、strtod,但是当数组太大时它们会非常耗时,因为它们会调用strlen()。而且sscanf也很慢......
我调试代码,发现atof()和strtod都调用了Visual Studio中的strlen(),我们可以查crt代码。
strtod() call:
answer = _fltin2( &answerstruct, ptr, (int)strlen(ptr), 0, 0, _loc_update.GetLocaleT());
atof() call:
return( *(double *)&(_fltin2( &fltstruct, nptr, (int)strlen(nptr), 0, 0, _loc_update.GetLocaleT())->dval) );
我也尝试使用strtok,但我们不应该更改1.txt中的任何数据。
所以任何人都有最好的方法将所有这些转换为浮点数 x、y、z。
Visual Studio 2008 + WIN7
I got a char array, a huge array char p[n] read from a txt like.
//1.txt 194.919 -241.808 234.896 195.569 -246.179 234.482 194.919 -241.808 234.896 ...
foo(char *p, float x, float y, float z)
{
}
I tried to use atof, strtod, but they are real time consuming when the array is too huge, because they will call the strlen(). and the sscanf is also very slow....
I debug into the code and find that both atof() and strtod call the strlen() in the visual studio, we can check the crt code.
strtod() call:
answer = _fltin2( &answerstruct, ptr, (int)strlen(ptr), 0, 0, _loc_update.GetLocaleT());
atof() call:
return( *(double *)&(_fltin2( &fltstruct, nptr, (int)strlen(nptr), 0, 0, _loc_update.GetLocaleT())->dval) );
I also try to use strtok, but we should not change any data in the 1.txt.
so any one have the best way to convert all these to float x, y, z.
Visual studio 2008 + WIN7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您可以对浮点值的格式做出额外的假设,那么自己解析它们可能会提高性能。
用于解析不带指数且不进行输入验证的
' '
或'\n'
分隔值的示例代码:示例代码:
If you can make additional assumptions about the format of the floating point values, parsing them yourself might increase performance.
Example code for parsing
' '
or'\n'
-separated values without exponents and no input validation:Example code:
好的,您自己进行标记化然后调用 strtod 怎么样?
我的想法是这样的:
一些问题是标记化非常简单。它适用于您发布的格式,但如果格式不同(另一行使用 : 来分隔数字),它将不起作用。
另一个问题是代码假设所有数字都 < 64 个字符。如果它们更长,就会出现缓冲区溢出。
另外,复制到临时缓冲区会增加一些开销(但希望少于在整个缓冲区上不断执行 strlen 的开销)。我知道你说你不能改变原来的缓冲区,但是你可以做一个临时的改变(即只要你在返回之前将其恢复到原始状态,缓冲区就可以改变):
这种技术意味着没有复制,没有后顾之忧关于缓冲区溢出。您确实需要临时修改缓冲区;希望那是
Okay, how about doing the tokenization yourself and then calling strtod.
What I'm thinking is something like this:
Some issues with this is the tokenization is very simple. It will work for the format you posted, but if the format varies (another line uses : to separate the numbers), it won't work.
Another issue is that the code assumes all numbers have < 64 characters. If they are longer, you'll get a buffer overflow.
Also, the copying to a temporary buffer will add some overhead (but hopefully less then the overhead of constantly doing a strlen on the entire buffer). I know you said you can't change the original buffer, but can you do a temporary change (i.e. the buffer can change as as long as you return it to it's original state before you return):
This technique means no copying and no worries about buffer overflow. You do need to temporarily modify the buffer; hopefully that is
查看此代码。
如果不需要支持科学表示法、“+”号或前导制表符,则可以进一步优化。
它不使用 strlen 或任何其他标准库字符串例程。
Check out this code.
It can be further optimized if there's no need to support scientific representation, '+' sign, or leading tabs.
It doesn't use strlen, or any other standard library string routine.
只要您没有使用特别糟糕的标准库(现在不可能,它们都很好),就不可能比
atof
更快。As long as you are not using a particularly bad standard library (impossible these times, they are all good) it's not possible to do it faster than
atof
.我不明白为什么
strod()
应该调用strlen()
。当然它可能,但它的规范中没有任何内容要求它,如果它确实如此,我会感到惊讶。我想说strtod()
的速度与您所能达到的速度一样快,除非您自己编写一些 FPU 处理器特定的东西。I don't see any reason why
strod()
should callstrlen()
. Of course it might, but nothing in its specification requires it and I'd be suprised if it did. And I'd say thatstrtod()
about as fast as you'll get, short of writing some FPU processor-specific stuff yourself.为什么你认为atof、strtod使用strlen?我从未实现过它们,但我无法想象为什么他们需要知道输入字符串的长度。这对他们来说没有任何价值。我会按照 Jason 的回答使用 strtod 。这就是它的用途。
是的,如果您有大量文本,则需要一些时间来转换。事情就是这样。
Why do you think atof, strtod use strlen? I've never implemented them, but I can't imagine why they'd need to know the length of the input string. It would be of no value to them. I'd use strtod as per Jason's answer. That's what it's for.
And yes, if you have a very large amount of text, it's going to take some time to convert. That's just the way it is.
使用
strtod
。它几乎肯定不会调用strlen
。为什么需要知道输入的长度?它只是运行过去的前导空格,然后消耗尽可能多的对浮点文字有意义的字符,然后返回刚刚过去的指针。您可以查看示例实现也许您使用它时效果不佳?以下是如何使用strtod
的示例:输出:
在我的机器上。
Use
strtod
. It almost certainly does not callstrlen
. Why would it need to know the length of the input? It merely runs past leading whitespace, then consumes as many characters as possible that make sense for a floating point literal, and then returns a pointer just past that. You can see an example implementation Perhaps you're using it non-optimally? Here's a sample of how to usestrtod
:This outputs:
on my machine.
正如其他人所说,我认为您不会比标准库调用做得更好。它们已经存在很长时间并且经过了高度优化(嗯,它们应该是这样,至少在良好的实现中是这样)。
也就是说,有些事情我不清楚。您是否将整个文件读入内存,然后将数组转换为另一个数组?如果是这样,您可能需要检查正在运行的系统是否有足够的内存来执行交换。如果您这样做,当您从磁盘读取它们而不是存储它们时,是否可以一次只转换一行?
您可以考虑对程序进行多线程处理。一个线程从磁盘读取和缓冲行,n 个线程处理这些行。 Dr. Dobb's Journal 发表了一篇伟大的单读者/单作者您可以使用无锁队列实现。我在类似的应用程序中使用过它。我的工作线程每个都有一个输入队列,然后读取线程从磁盘读取数据并以循环方式将它们放入这些队列中。
As others have said, I don't think you're going to do much better than the standard library calls. They have been around for a long time and are quite highly optimized (well, they should be, at least in good implementations).
That said, there are some things that aren't clear to me. Are you reading the whole file into memory and then converting the array to another array? If so, you might want to check that the system you are running on has enough memory to do that with swapping. If you are doing this, would it be possible to just convert one line at a time as you read them off disk instead of storing them?
You could consider multithreading your program. One thread to read and buffer lines off disk, and n threads to process the lines. Dr. Dobb's Journal published a great single-reader/single-writer lockless queue implementation you could use. I've used this in a similar app. My worker threads each have an input queue, and then reader thread reads data off disk and places them into these queues in round robin style.
怎么样:
应该是三位有效数字。
Edit: watch out for big mantissas.
Edit again: and negative exponents. :-(
How about something like:
Should be good to three significant digits.
Edit: watch out for big mantissas.
Edit again: and negative exponents. :-(
我怀疑
strlen
是否会花费您很多钱。如果你可以利用你的数字落在相对有限的范围内,那么我建议你自己解析它,尽可能少地进行计算,例如:
I doubt if
strlen
is costing you much.If you can take advantage of your numbers falling in a relatively restricted range, then what I suggest is to parse it yourself, doing as little computation as possible, such as: