Re: Comment on trim string function please




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



.



Relevant Pages

  • Re: loop performance question
    ... | such that every possible pair in the array is tested. ... | whenever I access any data in the second loop. ... The pointers to separately allocated objects break the ... Then comes the question of how to avoid cache misses when accessing obj2. ...
    (comp.lang.cpp)
  • Re: Problems with a secondary message pump
    ... it says that the dwWakeMask is 'the input types for ... array that I don't want to stop the loop on. ... was just a guess that OnIdle is freeing them, ... getting pointers to the wizard's system menu and Cancel button and storing ...
    (microsoft.public.vc.mfc)
  • Re: newbie question
    ... The second assigns a pointer to an array 1000 of char to an int pointer. ... What you pass are the file pointers, ... > second file is identical to the first. ... Look at your while loop. ...
    (comp.lang.c)
  • Re: GC.Collect
    ... Thanks for the suggestions / pointers, they will definitely help me in making a wise decision. ... No major allocations are done in that loop, essntially not a very heavy work per iteration. ... Well for each 65'000 of those objects the gen0 in .NET 1.1 will get full, the GC kicks in and move most of them to gen1. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: cannot understand strrev implementation
    ... This loop finds the last character in the string. ... 'string' pointers, and then moves them towards each other, stopping when ... Ask your assistant to swap the cards that you are pointing to. ...
    (comp.lang.c)