is there any faster way to parse string into float number

Discussion in 'C++' started by Python.LeoJay@gmail.com, Mar 16, 2006.

  1. Guest

    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
    , Mar 16, 2006
    #1
    1. Advertising

  2. wrote:

    > 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
    Mathias Waack, Mar 16, 2006
    #2
    1. Advertising

  3. Guest

    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
    , Mar 16, 2006
    #3
  4. Col Guest

    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

    wrote:
    > 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
    Col, Mar 16, 2006
    #4
  5. wrote:
    > 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()'.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Mar 16, 2006
    #5
  6. Col <> wrote:

    > wrote:


    >> 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.


    > 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
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Mar 16, 2006
    #6
  7. Leo jay Guest

    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!!!
    Leo jay, Mar 16, 2006
    #7
  8. Leo jay wrote:
    > 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()'...
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Mar 16, 2006
    #8
  9. Leo jay wrote:
    > 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()'?
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Mar 16, 2006
    #9
  10. benben Guest

    wrote:
    > 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
    benben, Mar 17, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steve
    Replies:
    17
    Views:
    681
    Mike Smith
    Sep 13, 2004
  2. bd
    Replies:
    0
    Views:
    603
  3. Replies:
    19
    Views:
    1,101
    Daniel Vallstrom
    Mar 15, 2005
  4. nas
    Replies:
    10
    Views:
    497
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 19, 2007
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,503
    James Kanze
    Oct 8, 2009
Loading...

Share This Page