Re: Newby C++ vs. C question
alexrixhardson@yahoo.com wrote:
I am a newby in the C/C++ world, and I am beginning to work on a
rather simple TCP/IP proxy application which must be able to handle
large volume of data as quickly as possible.
Since this application has to process and distribute plain text around
the network, I am wondering if there are any peformance differences
between C++ std::string and C char[] in run time?
A program that distributes large amounts of text around the network is
likely IO bound. I doubt if any micro-optimizations on memory
allocation/access will have much of a performance impact.
Which one would you suggest me to use for my particular task (TCP/IP
proxy which is distributing plain text around the nextwork that
is :-) )?
std::string is much easer to use all around so that is what I suggest
you use (as a newbie or not.) If you, or someone on your team, have some
experience in the language, you might want to consider using a string
class that is specialized for large strings. SGI's "rope" class might
work better.
One thing you might do is wrap std::string in your own class so that you
can easily change to a different implementation later if necessary.
p.s.: here're two examples that I found on the Internet for which I am
wondering if there are any performance differences between them:
==========================
C function returning a copy
==========================
char *fnConvert(int _ii)
{
char *str = malloc(10); /* Return 10 character string */
if(str == NULL)
fprintf(stderr,"Error: Memory allocation failed.\n");
sprintf(str, "%d", _ii);
return str;
}
==========================
C++ function returning a copy
==========================
string fnConvert(int _ii)
{
ostringstream ost;
ost << _ii;
return ost.str();
}
It's an unfair comparison. The later function does much more than the
former one does. I suggest you use the later function though because it
does more, in fewer lines.