473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Comments reqd

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
7 1384

"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
"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
"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
<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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: lkrubner | last post by:
I've got a PHP application that's 2 megs in size. Of that, my guess is 200k-400k is comments. Do they impose a performance hit? I've been postponing any kind of optimization, but at some point I'll...
4
by: Uwe Ziegenhagen | last post by:
Hello, my fellows and me implement a c++ tool that is able to divide blank/tab separated files into <number>, <text>, <c-singlelinecomment> and <multilinecomment>. So far it's not working bad,...
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
40
by: Edward Elliott | last post by:
At the risk of flogging a dead horse, I'm wondering why Python doesn't have any multiline comments. One can abuse triple-quotes for that purpose, but that's obviously not what it's for and doesn't...
7
by: Bob Stearns | last post by:
Several weeks ago I asked what comments I could pass to DB2 in a SELECT statement. I don't remember whether I said via PHP/ODBC. I was assured that both /* */ style comment blocks and -- comment...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
3
by: altafur | last post by:
hi, i am using java with hibernate . i want to run a hibernate order by query thru java. i have written the query in hibernate.hbm.xml file. the query is as follows: query: SELECT distinct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.