Macro for atoi similar function

A

Allan Bruce

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)
 
A

Allan Bruce

Allan Bruce said:
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; \
} \
} \
}
 
A

Allan Bruce

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; \
}
 
A

Artie Gold

Allan said:
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
 
A

Artie Gold

Allan said:
it is


=---

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
 
T

those who know me have no need of my name

in comp.lang.c i read:
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

Allan Bruce

those who know me have no need of my name said:
in comp.lang.c i read:


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

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
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top