Newby C++ vs. C question

A

alexrixhardson

Hi guys,

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?

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 :) )?

Thanks, Alex

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();
}
 
V

Victor Bazarov

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?

Yes, there are. Not significant, but some.
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 :) )?

Sorry, I don't do C, so suggest C++ regardless of the task.
Thanks, Alex

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();
}

Yes, there are differences, in general. As to _what_ those are, you
would need to measure to see.

V
 
D

Default User

Hi guys,

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?

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 :) )?

Thanks, Alex

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();
}


Obviously, the C++ version is simpler, because the implementation does
a lot of the work under the hood. As far as efficiency or speed or
anything else, as Victor says, you'd have to test.

Disregarding optimization, the C++ version would do seem a bit more
work, what with allocating in the first place, then copying the string
for return. However, the compiler might well do things more
efficiently, so there may be little difference, or the C++ version
might be better. No way to tell.

When working in C or C++ with plain arrays, you can often improve
efficiency by not doing dynamic allocations at all. It would depend on
the specific application, but for networking there's often one send
buffer declared at the start, and all new messages built in that.

In most modern systems, the hardware has gotten so good, and the
compilers so clever, that micro-optimization of the code isn't needed.
Do you really care if the program takes 600 milliseconds to execute,
rather than 200?

In general, you should work in the language and style that is most
comfortable and most clearly expresses what you want. If performance is
acceptable, then you're done. Otherwise, profile and correct
bottlenecks.



Brian
 
J

James Kanze

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.

That sounds like a contradiction to me. A TCP/IP proxy
application requires an expert. (And some other people, of
course. Even in the most critical applications, a good deal of
the work can be done by people who are average, or even less.)
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?

Obviously. They do different things, and when you do different
things, there will be differences in runtime. If you use them
to do the same thing, performance should be similar.
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, until the profiler says differently.
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();
}

I don't know about performance, but I do know that they do
radically different things. The first one core dumps for some
input, at least on my machine, and the second one doesn't. And
even when the first one doesn't core dump, it leaks memory; call
it often enough, and you're run out of memory.

As I said, C style strings and std::string do different things.
 
D

Daniel T.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JNI: C vs C++ 2
Short Questions on C++ 6
TF-IDF 1
Is this valid C++ code? 6
C to C++ Syntax 8
ChatBot 4
Question concerning array.array and C++ 0
formatting buffers 3

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top