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