By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

Macro for atoi similar function

P: n/a
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)
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:be**********@news.freedom2surf.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; \
} \
} \
}
Nov 13 '05 #2

P: n/a
>
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; \
}
Nov 13 '05 #3

P: n/a
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 =---
Nov 13 '05 #4

P: n/a
Allan Bruce wrote:
"Artie Gold" <ag***@bga.com> wrote in message
news:3F**************@bga.com...
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 =---
Nov 13 '05 #5

P: n/a
in comp.lang.c i read:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:be**********@news.freedom2surf.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
Nov 13 '05 #6

P: n/a

"those who know me have no need of my name" <no****************@usa.net>
wrote in message news:m1*************@usa.net...
in comp.lang.c i read:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:be**********@news.freedom2surf.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
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.