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

Comments reqd

P: n/a
Hello Group,
I have wriiten a code for finding whitespaces from a source
string and create a new destination string by replacing the whitespaces with
a new string("%20",in this case).The destination string is allocated more
than the source string length by an amount which is assumed as the maximum
number of whitesapce that may occur.and also the first character is set to
'\0';
ie In this case *pcDest = '\0';
I want to know about the optimization done whether it is worthwhile or not.

Generally the whitespaces do not occur so often
if(search == NULL) is wriiten first.Does this improve performance

void RemoveWhiteSpaces(char *pcSrc,char *pcDest)
{
while(*pcSrc)
{
char *search = strchr(pcSrc,' ');
if(search == NULL)
{
strcat(pcDest,pcSrc);
break;
}
else
{
strncat(pcDest,pcSrc,search - pcSrc);
pcDest += search - pcSrc;
pcSrc += search - pcSrc + 1;
strcat(pcDest,"%20");
pcDest += 3;
}
}
}

Is there any better method of doing this?

Please don't be harsh. I am still a newbie.

Thanks in advance.

Regards,
Naren.

Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Naren" <na**************@de.bosch.com> wrote in message
news:bq**********@ns2.fe.internet.bosch.com...
Hello Group,
I have wriiten a code for finding whitespaces from a source string and create a new destination string by replacing the whitespaces with a new string("%20",in this case).The destination string is allocated more
than the source string length by an amount which is assumed as the maximum
number of whitesapce that may occur.and also the first character is set to
'\0';
ie In this case *pcDest = '\0';
I want to know about the optimization done whether it is worthwhile or not.
Generally the whitespaces do not occur so often
if(search == NULL) is wriiten first.Does this improve performance

void RemoveWhiteSpaces(char *pcSrc,char *pcDest)
{
while(*pcSrc)
{
char *search = strchr(pcSrc,' ');
if(search == NULL)
{
strcat(pcDest,pcSrc);
break;
}
else
{
strncat(pcDest,pcSrc,search - pcSrc);
pcDest += search - pcSrc;
Rewritting the above statement.

pcDest +=(search-pcSrc); This guarantees that the pointer is added to an
offset. If I am correct pointer addition is not advisable, although pointer
substraction is. Please note this will not have any performance improvement.
This is just a comment.

Any comments on this.
pcSrc += search - pcSrc + 1;
strcat(pcDest,"%20");
pcDest += 3;
}
}
}

Is there any better method of doing this?

Please don't be harsh. I am still a newbie.

Thanks in advance.

Regards,
Naren.

Nov 13 '05 #2

P: n/a
"Naren" <na**************@de.bosch.com> wrote:
I have wriiten a code for finding whitespaces from a source
string and create a new destination string by replacing the whitespaces with
a new string("%20",in this case).The destination string is allocated more
than the source string length by an amount which is assumed as the maximum
number of whitesapce that may occur.
Be careful. Some day, someone will pass " " as an
experiment. Make it the maximum that _can_ occur, not that you assume
will occur.
I want to know about the optimization done whether it is worthwhile or not.

Generally the whitespaces do not occur so often
if(search == NULL) is wriiten first.
It is impossible to say in advance whether this optimisation is worth
the trouble or not. In this case, swapping the if and else clauses
wouldn't make the code significantly more or less clear, so you may as
well leave it this way if you expect that on your system it does run
faster.
It is important to realise, however, that the Standard makes no demands
whatsoever about efficiency, and it would be reasonable for your code to
be more efficient than the alternative on one system, less so on
another, and for it to make no difference at all on another.
If you want a real answer, measure. Compile both versions, give them
some serious work to do, and measure which actually turns out to be
faster. Don't be surprised if the answer turns out to be "this one, but
only by a nanosecond for each space replaced", or "neither, within our
measuring capability"; and whatever the outcome, remember that it is
valid for the tested system, and for the tested system _only_.
void RemoveWhiteSpaces(char *pcSrc,char *pcDest)
{
while(*pcSrc)
{
char *search = strchr(pcSrc,' ');
if(search == NULL)
{
strcat(pcDest,pcSrc);
break;
}
else
{
strncat(pcDest,pcSrc,search - pcSrc);
pcDest += search - pcSrc;
pcSrc += search - pcSrc + 1;
strcat(pcDest,"%20");
pcDest += 3;
}
}
}

Is there any better method of doing this?


It might or might not be more efficient to declare search at the top of
the function instead of inside the list. To an optimising compiler,
though, it shouldn't matter at all.
It might or might not be faster to change pcDest once if you find a
space, like this:

strncat(pcDest,pcSrc,search - pcSrc);
pcDest += search - pcSrc + 3;
pcSrc += search - pcSrc + 1;
strcat(pcDest-3, "%20");

It probably won't be, though.

Richard
Nov 13 '05 #3

P: n/a
"user" <no**@none.com> wrote:
"Naren" <na**************@de.bosch.com> wrote in message
news:bq**********@ns2.fe.internet.bosch.com...
pcDest += search - pcSrc;


Rewritting the above statement.

pcDest +=(search-pcSrc); This guarantees that the pointer is added to an
offset.


The way the Standard defined the += operator already guarantees that.
There is no need for the parentheses at all.
If I am correct pointer addition is not advisable, although pointer
substraction is.


Well... adding two pointers isn't valid at all, let alone advisable.
Luckily, two pointers are not added anywhere in this statement.

Richard
Nov 13 '05 #4

P: n/a
<SNIP>
Is there any better method of doing this?

It might or might not be more efficient to declare search at the top of
the function instead of inside the list. To an optimising compiler,
though, it shouldn't matter at all.
It might or might not be faster to change pcDest once if you find a
space, like this:

strncat(pcDest,pcSrc,search - pcSrc);
pcDest += search - pcSrc + 3;
pcSrc += search - pcSrc + 1;
strcat(pcDest-3, "%20");

It probably won't be, though.

Richard

<SNIP>
Thank you very much Richard.
I cannot compile and check since it is a very big program and I am supposed
to insert this small piece of code.
But I will consider the last part which you have shown.
Thank you once again.

Regards,
Naren.

Nov 13 '05 #5

P: n/a
"Naren" <na**************@de.bosch.com> wrote:
Hello Group,
I have wriiten a code for finding whitespaces from a source
string and create a new destination string by replacing the whitespaces with
a new string("%20",in this case).The destination string is allocated more
than the source string length by an amount which is assumed as the maximum
number of whitesapce that may occur.and also the first character is set to
'\0';
ie In this case *pcDest = '\0';
I want to know about the optimization done whether it is worthwhile or not.

Generally the whitespaces do not occur so often
if(search == NULL) is wriiten first.Does this improve performance
Well, you let strchr inspect the source string first, just to decide
how much of it to copy afterwards. Why not iterate the string and
do the check/copy/replacement 'on the fly'? It'll save you some
function calls and pointer calculations (which BTW make your code less
readable, IMHO). However, I'm not sure about performance, it depends
on the implementation and the optimizations performed by the compiler.
void RemoveWhiteSpaces(char *pcSrc,char *pcDest)
{
while(*pcSrc)
{
char *search = strchr(pcSrc,' ');
if(search == NULL)
{
strcat(pcDest,pcSrc);
break;
}
else
{
strncat(pcDest,pcSrc,search - pcSrc);
pcDest += search - pcSrc;
pcSrc += search - pcSrc + 1;
strcat(pcDest,"%20");
pcDest += 3;
}
}
}

Is there any better method of doing this?


Here's at least /another/ way to implement the function:

#include <string.h>

char *ReplaceBlanks( char *dest, const char *src )
{
char *dst = dest;

while ( *src )
{
if ( *src != ' ' )
{
*dst++ = *src++;
}
else
{
dst = strcpy( dst, "%20" ) + 3;
++src;
}
}
*dst = '\0';

return dest;
}

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6

P: n/a
"Naren" <na**************@de.bosch.com> wrote:
It might or might not be more efficient to declare search at the top of
the function instead of inside the list. To an optimising compiler,
though, it shouldn't matter at all.
I cannot compile and check since it is a very big program and I am supposed
to insert this small piece of code.


Well, you could always write a test program which just runs your
function over a large amount of text and times it; and do the same for
the alternative function. Better beware of disk and memory caches,
though; it's probably best to run the test program a couple of times and
only take the last measurements.

Richard
Nov 13 '05 #7

P: n/a
Well, you let strchr inspect the source string first, just to decide
how much of it to copy afterwards. Why not iterate the string and
do the check/copy/replacement 'on the fly'? It'll save you some
function calls and pointer calculations (which BTW make your code less
readable, IMHO). However, I'm not sure about performance, it depends
on the implementation and the optimizations performed by the compiler.


Yes, you are right.Thank you.
I had completely forgotten that I was actaully making additional calls.Thank
you for the wonderful code as well.

Best Regards,
Naren.
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.