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) 6 4731
"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; \
} \
} \
}
> 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 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 =---
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 =---
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robbie |
last post by:
Someone tell me,
if not exist, how to write one with the same function?
I have made a try, but there is no character type in python,
so i failed, someone help me,
thanks very much
|
by: Andrew Dalke |
last post by:
Here's a proposed Q&A for the FAQ based on a couple recent
threads. Appropriate comments appreciated
X.Y: Why doesn't Python have macros like in Lisp or Scheme?
Before answering that, a...
|
by: Mike Moum |
last post by:
I think there may be a bug in string.atoi and string.atol. Here's some
output from idle.
> Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
> on linux2
> Type "copyright", "credits" or "license()"...
|
by: Bansidhar |
last post by:
atoi() function seems not to have any support for Hex, octal number.
Usually when I read from a text file then it contain number like
0x232 etc. In this case atoi() fells. In case of itoa() there...
|
by: Simon Morgan |
last post by:
Hi,
Can somebody please help me grok the offsetof() macro?
I've found an explanation on
http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031
but I'm afraid it still...
| |
by: sudharsan |
last post by:
Hi
could you please explain wat atoi( ) function is for and an example
how to use it?
|
by: CptDondo |
last post by:
Could someone please let me know the equivalent of C atoi and similar
functions? I need to convert a hex text string - e.g. '0x01' - to its
integer equivalent.
I also want to be able to print...
|
by: ptq2238 |
last post by:
Hi,
I have written this code to help me learn C but I'm not sure why gcc -
Wall is giving me an error when I compile
Basically I want to read in a character then a number and then
manipulate...
|
by: Ram |
last post by:
Hi All,
Firstly i am a newbie and trying to learn C.
The background of the problem is
Program:
Presently I am working on a program of numerology and the I/P will be
the name and output...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |