is there any faster way to parse string into float number

P

Python.LeoJay

Dear all,

i need to parse billions of numbers from a file into float numbers for
further calculation.
i'm not satisfied with the speed of atof() function on my machine(i'm
using visual c++ 6).

so i wonder is there any faster way to parse string(char array) into
float number.

Thanks
Leo Jay
 
M

Mathias Waack

i need to parse billions of numbers from a file into float numbers for
further calculation.
i'm not satisfied with the speed of atof() function on my machine(i'm
using visual c++ 6).

so i wonder is there any faster way to parse string(char array) into
float number.

Maybe. atof() has to handle all possible formats - thus if you know the
format of your strings you should be able to write a faster parser
specialised for your own needs.
But what exactly is your c++-question?;)

Mathias
 
P

Python.LeoJay

thanks for you reply.

any kind of valid float number expression would be possible.
here is a excerpt from one of my files for example: "8.2109000000e+04
-2.8705000000e+04 0 0 0.800000 0.270000 2.160000 9.9000000000e-01"
so, i have to handle all possible formats too. :(


as far as i know, there is a c++ standard header file named cmath, and
there is also a function which is defined in cmath named atof.
so, my c++ question is how to speed up the c++ function named atof? or
is there any decent c++ way to achieve my purpose. ;)

thanks a million.
Leo Jay
 
C

Col

If some how you make char* (i.e creating a string object by passing
char*) to string object in c++, and pass those object to this func. it
can solve ur problem.

float value(string str)
{
istream ob;
ob>>str;
return str;
}


Regards,
Apoorv
 
D

Dietmar Kuehl

as far as i know, there is a c++ standard header file named cmath, and
there is also a function which is defined in cmath named atof.

That's just C's 'atof()' function made available through namespace
'std'. Also, 'atof()' is not in <cmath> but in <cstdlib>.

Also, I doubt that you can squeeze a major performance gain out of
'atof()'.
 
J

Jakob Bieling

Col said:
(e-mail address removed) wrote:
float value(string str)
{
istream ob;
ob>>str;
return str;
}

I doubt that this will be faster than using atof. Besides the fact
that this will not even work, you will be constructing a temporary
string object and a temporary istream object here. This is not what
makes things faster.

If the OP proved atof to be the bottleneck, he should try writing a
faster version himself. Since you need all the functionality atof gives
you, I agree this might be a bit difficult. You might be able to reduce
the number of function calls tho:

You said you have a string "8.2109000000e+04 -2.8705000000e+04 0 0
0.800000 0.270000 2.160000 9.9000000000e-01". So instead of parsing
every float seperately and finding the next space, you could have your
function parse all floats at once and store the result in an array. This
way you will 1) decrease the number of parameters you have to pass the
functions, 2) decrease the number of copied return values and 3) are
also able to have your function return the pointer *after* the parsing:

char* atofs (char* buf, std:vector <float>& out)
{
while we have something left to parse
{
parse the float in 'buf'

if parsing went alright
out.push_back (the resulting float);
else
break;
}

return buf;
}

Obviously, the above is pseudo-code. Note that the more floats you
will pass at once (ie. the longer your string for parsing is), the
greater the speed difference might be. But there is no guarantee that
anything will be faster afterwards. Nonetheless, I would take the chance
and just try it.

hth
 
L

Leo jay

To Col: i don't think the istream is faster than atof, just as cout is
much lower than printf.

To Dietmar Kuehl: but in visual c++ 6.0, i found the atof in math.h
which is included in cmath.

To Jakob Bieling: that's a good idea, i will try it. thanks.

Thank all of you for your help!!!
 
D

Dietmar Kuehl

Leo said:
To Dietmar Kuehl: but in visual c++ 6.0, i found the atof in math.h
which is included in cmath.

Well, the standard location of 'atof()' is <cstdlib> or <stdlib.h>.
It may also be defined in <math.h> and/or in <cmath> but relying on
this will render your program non-portable.

However, from what you have said, you are probably looking into
different issues than portability with respect of the location of
'atof()'...
 
D

Dietmar Kuehl

Leo said:
To Jakob Bieling: that's a good idea, i will try it. thanks.

I doubt that the processing of multiple floating point values will
make much of a difference. However, here are some hints on what
indeed might make some difference:

- 'atof()' is supposed to cope with really *all* kinds of floating
point values, including hexadecimal representations (there are
used for exact external representation of IEEE floating point
values). If you, at least, don't use these, you can safe a little
bit of preprocessing to figure out the format.

- The example value you have shown exhibit only a relatively small
number of significant digits - most of them are just meaningless
zeros. If it is acceptable to have only 'log(10) ULONG_MAX' (i.e.
for typical 32 bit machines nine) significant decimal digits
after the decimal point, you can deal with an unsigned long to
represent the fraction part of the mantissa. A similar argument
cannot applied to the integer part unless the floating point
format is known to procude scientific notation if the number of
integer digits exceeds e.g. six.

In addition, I actually doubt that 'atof()' is indeed you bottleneck.
Unless the system you are using has really fast I/O, your actual
bottleneck is more than likely I/O rather than 'atof()'. Have you
profiled your application and traced your performance problem to the
use of 'atof()'?
 
B

benben

Dear all,

i need to parse billions of numbers from a file into float numbers for
further calculation.
i'm not satisfied with the speed of atof() function on my machine(i'm
using visual c++ 6).

so i wonder is there any faster way to parse string(char array) into
float number.

Thanks
Leo Jay

Write yourself a function that specializes in string-to-float
conversion. If you have difficulties grab a book on compiler or parser
and read about it.

But I doubt significant performance gain over atof can be achieved,
though it is worth while to try.

Also, do an analysis on the pattern of the strings in the file. Some
optimization can sometimes be done by caching the most recent
calculations, etc.

Regards,
Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top