Macro for atoi similar function

Discussion in 'C Programming' started by Allan Bruce, Jul 4, 2003.

  1. Allan Bruce

    Allan Bruce Guest

    Hi there,
    I am loading in a 3d object from file. The code I have works fine but it is
    very slow for objects with many vertices, i.e. >5000
    I make use of the atoi function 3 times for each vertex, so am making a lot
    of them! I wish to make a macro to speed up execution. I know that the
    numbers will all be stored correctly in a char array in memory. I also know
    that each number will be >0 and I will limit the value to less then the max
    value of UINT on my machine.
    I have made macros before, but very simple ones. I want to make a macro for
    the above definition. My (poor) attempt is below. I know I am well off as
    far as macros go, but does anyone have any suggestions as to how I should
    proceed?
    Many Thanks
    Allan

    #define ATOI(X) ( temp = 0; \
    while(1){ \
    if ((*X >='0') && (*X <='9')) \
    { \
    temp*=10; \
    temp += *X -'0'; \
    X++; \
    } \
    else \
    { \
    X++; \
    break; \
    } \
    } \
    temp)
    Allan Bruce, Jul 4, 2003
    #1
    1. Advertising

  2. Allan Bruce

    Allan Bruce Guest

    "Allan Bruce" <> wrote in message
    news:be43dn$9j9$2surf.net...
    > Hi there,
    > I am loading in a 3d object from file. The code I have works fine but it

    is
    > very slow for objects with many vertices, i.e. >5000
    > I make use of the atoi function 3 times for each vertex, so am making a

    lot
    > of them! I wish to make a macro to speed up execution. I know that the
    > numbers will all be stored correctly in a char array in memory. I also

    know
    > that each number will be >0 and I will limit the value to less then the

    max
    > value of UINT on my machine.
    > I have made macros before, but very simple ones. I want to make a macro

    for
    > the above definition. My (poor) attempt is below. I know I am well off

    as
    > far as macros go, but does anyone have any suggestions as to how I should
    > proceed?
    > Many Thanks
    > Allan
    >
    > #define ATOI(X) ( temp = 0; \
    > while(1){ \
    > if ((*X >='0') && (*X <='9')) \
    > { \
    > temp*=10; \
    > temp += *X -'0'; \
    > X++; \
    > } \
    > else \
    > { \
    > X++; \
    > break; \
    > } \
    > } \
    > temp)
    >
    >


    I figured it out, now I need to make a similar macro for strtod()

    #define ATOI(X, result, leng) \
    { \
    char *lptr = X; \
    result = 0; \
    leng = 0; \
    while (1) \
    { \
    if ((*lptr >= '0') && (*lptr <= '9')) \
    { \
    result *= 10; \
    result += *lptr - '0'; \
    lptr++; \
    leng++; \
    } \
    else \
    { \
    break; \
    } \
    } \
    }
    Allan Bruce, Jul 4, 2003
    #2
    1. Advertising

  3. Allan Bruce

    Allan Bruce Guest

    >
    > I figured it out, now I need to make a similar macro for strtod()
    >
    > #define ATOI(X, result, leng) \
    > { \
    > char *lptr = X; \
    > result = 0; \
    > leng = 0; \
    > while (1) \
    > { \
    > if ((*lptr >= '0') && (*lptr <= '9')) \
    > { \
    > result *= 10; \
    > result += *lptr - '0'; \
    > lptr++; \
    > leng++; \
    > } \
    > else \
    > { \
    > break; \
    > } \
    > } \
    > }
    >
    >


    Just incase anybody is interested, here is an strtod() type macro

    #define STRTOD(X, result, leng) \
    { \
    boolean neg = FALSE; \
    int fraction = 0; \
    int after = 0; \
    char *lptr = X; \
    result = 0; \
    leng = 0; \
    if (*lptr == '-') \
    { \
    lptr++; leng++; neg = TRUE; \
    } \
    while(1) \
    { \
    if ((*lptr >= '0') && (*lptr <='9'))\
    { \
    result *= 10; \
    result += *lptr - '0'; \
    lptr++; leng++; \
    } \
    else \
    break; \
    } \
    if (*lptr == '.')/* fraction part follows*/ \
    { \
    lptr++; leng++; /*skip fraction part */ \
    while(1) \
    { \
    if ((*lptr >= '0') && (*lptr <='9')) \
    { \
    after *= 10; \
    after += *lptr -'0'; \
    fraction++; lptr++; leng++; \
    } \
    else \
    { \
    result += ((double)after)/pow(10, fraction);\
    break; \
    } \
    } \
    } \
    if (neg) result = -result; \
    }
    Allan Bruce, Jul 4, 2003
    #3
  4. Allan Bruce

    Artie Gold Guest

    Allan Bruce wrote:
    > Hi there,
    > I am loading in a 3d object from file. The code I have works fine but it is
    > very slow for objects with many vertices, i.e. >5000
    > I make use of the atoi function 3 times for each vertex, so am making a lot
    > of them! I wish to make a macro to speed up execution. I know that the
    > numbers will all be stored correctly in a char array in memory. I also know
    > that each number will be >0 and I will limit the value to less then the max
    > value of UINT on my machine.
    > I have made macros before, but very simple ones. I want to make a macro for
    > the above definition. My (poor) attempt is below. I know I am well off as
    > far as macros go, but does anyone have any suggestions as to how I should
    > proceed?
    > Many Thanks
    > Allan
    >
    > #define ATOI(X) ( temp = 0; \
    > while(1){ \
    > if ((*X >='0') && (*X <='9')) \
    > { \
    > temp*=10; \
    > temp += *X -'0'; \
    > X++; \
    > } \
    > else \
    > { \
    > X++; \
    > break; \
    > } \
    > } \
    > temp)
    >
    >


    The efficacy of doing this aside -- not to mention the fact that it
    depends upon many assumptions about the data, etc. it is usually a good
    idea to wrap such macros in a do...while(0), e.g.:

    define SOME_MACRO_THAT_IS_A_BLOCK do { \
    /* some code */ \
    /* some more code */ \
    } \
    while (0)

    as it will now work correctly syntactically.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas



    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Artie Gold, Jul 4, 2003
    #4
  5. Allan Bruce

    Artie Gold Guest

    Allan Bruce wrote:
    > "Artie Gold" <> wrote in message
    > news:...
    >
    >>Allan Bruce wrote:
    >>
    >>>Hi there,
    >>>I am loading in a 3d object from file. The code I have works fine but

    >>

    > it is
    >
    >>>very slow for objects with many vertices, i.e. >5000
    >>>I make use of the atoi function 3 times for each vertex, so am making a

    >>

    > lot
    >
    >>>of them! I wish to make a macro to speed up execution. I know that the
    >>>numbers will all be stored correctly in a char array in memory. I also

    >>

    > know
    >
    >>>that each number will be >0 and I will limit the value to less then the

    >>

    > max
    >
    >>>value of UINT on my machine.
    >>>I have made macros before, but very simple ones. I want to make a macro

    >>

    > for
    >
    >>>the above definition. My (poor) attempt is below. I know I am well off

    >>

    > as
    >
    >>>far as macros go, but does anyone have any suggestions as to how I

    >>

    > should
    >
    >>>proceed?
    >>>Many Thanks
    >>>Allan
    >>>
    >>>#define ATOI(X) ( temp = 0; \
    >>> while(1){ \
    >>> if ((*X >='0') && (*X <='9')) \
    >>> { \
    >>> temp*=10; \
    >>> temp += *X -'0'; \
    >>> X++; \
    >>> } \
    >>> else \
    >>> { \
    >>> X++; \
    >>> break; \
    >>> } \
    >>> } \
    >>> temp)
    >>>
    >>>

    >>
    >>The efficacy of doing this aside -- not to mention the fact that it
    >>depends upon many assumptions about the data, etc. it is usually a good
    >>idea to wrap such macros in a do...while(0), e.g.:
    >>
    >>define SOME_MACRO_THAT_IS_A_BLOCK do { \
    >> /* some code */ \
    >> /* some more code */ \
    >> } \
    >> while (0)
    >>
    >>as it will now work correctly syntactically.
    >>
    >>HTH,
    >>--ag
    >>
    >>--
    >>Artie Gold -- Austin, Texas
    >>
    >>
    >>
    >>----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

    >
    > News==----
    >
    >>http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000

    >
    > Newsgroups
    >
    >>---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

    >
    > =---
    >
    > I know there is no error checking within the block here, but that is my
    > choice - I am convinced that there are no errors in the char array.
    > However, could you please elaborate with the do/while trick. Why does this
    > make it better?


    Sure. Because otherwise, if you write

    ATOI( something_or_other );

    it would expand to

    {
    ...
    /* your code */
    ...
    };
    ^

    The extra semicolon -- introducing an empty statement -- could wreak
    havoc in certain situations (within an if ... else construct, for
    example) that could be devilishly hard to diagnose.

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas



    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Artie Gold, Jul 4, 2003
    #5
  6. in comp.lang.c i read:
    >"Allan Bruce" <> wrote in message
    >news:be43dn$9j9$2surf.net...


    >> I am loading in a 3d object from file. The code I have works fine but
    >> it is very slow for objects with many vertices, i.e. >5000 I make use of
    >> the atoi function 3 times for each vertex, so am making a lot of them! I
    >> wish to make a macro to speed up execution.


    >I figured it out, now I need to make a similar macro for strtod()


    no, now you need to measure the performance gain you realized, to see if
    the time you spent was well spent, or wasted.

    --
    a signature
    those who know me have no need of my name, Jul 4, 2003
    #6
  7. Allan Bruce

    Allan Bruce Guest

    "those who know me have no need of my name" <>
    wrote in message news:...
    > in comp.lang.c i read:
    > >"Allan Bruce" <> wrote in message
    > >news:be43dn$9j9$2surf.net...

    >
    > >> I am loading in a 3d object from file. The code I have works fine but
    > >> it is very slow for objects with many vertices, i.e. >5000 I make use

    of
    > >> the atoi function 3 times for each vertex, so am making a lot of them!

    I
    > >> wish to make a macro to speed up execution.

    >
    > >I figured it out, now I need to make a similar macro for strtod()

    >
    > no, now you need to measure the performance gain you realized, to see if
    > the time you spent was well spent, or wasted.
    >
    > --
    > a signature


    Well, I did a macro for strtod() and an object with 30,000 polys took 110
    seconds to load, now it takes less than 0.25 secs!
    Not a bad increase - the question is why? I can think of 3 things
    1) Inline
    2) No error checking
    3) no e checking (10^x)
    Allan
    Allan Bruce, Jul 5, 2003
    #7
    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. lonelyplanet999
    Replies:
    8
    Views:
    42,924
  2. Mike Chirico
    Replies:
    2
    Views:
    3,821
    Grumble
    Nov 19, 2003
  3. mark
    Replies:
    2
    Views:
    18,244
    Victor Bazarov
    Jun 14, 2004
  4. Sonia

    atoi function

    Sonia, Feb 12, 2005, in forum: C++
    Replies:
    8
    Views:
    22,078
    Sonia
    Feb 13, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    457
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page