Home


Dennis Lang
lang.dennis @ comcast.net

Time to Convert Ascii String to Floating Point Number
Updated: 31-Aug-2009

While parsing xml, I discovered that converting a textual floating point value back to its binary form was expensive. Here are the results of various conversion solutions. Test run on Windows XP32, HP8600 2 Xeon Quad Core, using Visual Studio 2008.

Seconds Ratio Test Results
0.282 1.000 AToF 123456001.000
0.844 2.993 atof 123456001.000
0.891 3.160 strtod 123456001.000
1.125 3.989 scanf 123456001.282
8.000 28.369 strStream 123456001.000
9.344 33.135 stringStream 123456001.000

Ratio is elapsed seconds divided by AToF time.

AToF is a hand written version of the standard atof(). I found the code on the web at:

http://www.bsdlover.cn/study/UnixTree/V7/usr/src/libc/gen/atof.c.html

I am assuming the default atof() and strtod() are slower because they deal with language/locality issues.
StringStream is the slowest way to convert a string back to its binary form.

WARNING - atof() and strtod() internally call strlen() on the input string. If your input string is a pointer to a region inside a very large text document, it will take a long time to calculate the string length. I had a case where I was extracting floating point values from a file I memory mapped. To improve performance, I had to zero terminate each floating point character sequence. Since I did not want to alter the memory mapped file, I copied the text to a local buffer.


Home - Please visit home page for more programs and performance information.