Re: Comment on trim string function please
- From: "Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx>
- Date: Fri, 11 Jul 2008 01:29:46 GMT
Jens Thoms Toerring <jt@xxxxxxxxxxx> wrote in message
news:6dmve2F3ecqhU1@xxxxxxxxxxxxxxxxxxxx
swengineer001@xxxxxxxxx <swengineer001@xxxxxxxxx> wrote:
Just looking for a few eyes on this code other than my own.
for(j = 0; i < strlen(str); j++, i++)
{
str[j] = str[i];
}
An alternative would be to use memmove() here, so you don't
have to do it byte by byte.
What would be the actual metrics to make a decision here
one way or the other? I myself just assume for this kind of stuff
that it will only be done on relatively small strings, just a few
characters, so it seems that a call to memmove() might be overkill.
Or is it? DOES it depend on the size of the block you'll
be moving? How many cycles does it take to assign one
character to a previous array position, versus the overhead of
a call to memmove(), and the operation of the function itself?
Also callling strlen() each time
through the loop is a bit of a waste of time - it doesn't
change and can be replaced by a check if str[i] is '\0'.
Or just do a very simple while() loop that compares pointers:
char *start=str;
char *end=str+strlen(str);
while(start!=end) {
/* do something */
/* start++ for going forward from start */
/* end-- for going backwards from end */
}
Can't go wrong there...I will admit that I use to always use array
sub-scripting such as the OP because I found it easier conceptually
to understand, but then abandoned it largely for the equivalent
pointers as part of my paranoia about compiler "optimizations"
(I turn them ALL off, which means sub-scripting is not converted
to pointers, which I believe takes longer, so to retain the speed
I've manually converted most of the stuff like this).
---
William Ernest Reid
.
- Follow-Ups:
- Re: Comment on trim string function please
- From: DiAvOl
- Re: Comment on trim string function please
- From: Keith Thompson
- Re: Comment on trim string function please
- From: Willem
- Re: Comment on trim string function please
- From: voidpointer
- Re: Comment on trim string function please
- References:
- Comment on trim string function please
- From: swengineer001@xxxxxxxxx
- Re: Comment on trim string function please
- From: Jens Thoms Toerring
- Comment on trim string function please
- Prev by Date: Re: doubles and ints
- Next by Date: Re: doubles and ints
- Previous by thread: Re: Comment on trim string function please
- Next by thread: Re: Comment on trim string function please
- Index(es):
Relevant Pages
|