473,412 Members | 2,281 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,412 software developers and data experts.

Newby C++ vs. C question

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

Jul 31 '07 #1
5 1799
al************@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?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '07 #2
al************@yahoo.com wrote:
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

Jul 31 '07 #3
On Jul 31, 4:49 pm, alexrixhard...@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.
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 31 '07 #4
al************@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.
Jul 31 '07 #5
Thank you all for your comments. You made things more clear to me now.

Aug 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
0
by: Pete | last post by:
Hi All, A total Newby with, possibly, a daft question? However, until I can get a reasonable explanation I am disinclined towards going further. Here goes: I recently downloaded the latest...
8
by: Ask | last post by:
G'day All, Just thought I'd drop in and say hi. I'm new to Python, but old to software development. Python is one of the languages used in my new job, so I've just bought a book, read it, and...
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
4
by: Fred Nelson | last post by:
I have an applicatioin that I'm writing that uses a "case" file that contains over 350 columns and more may be added in the future. I would like to create a dataset with all the column names and...
4
by: Fred Nelson | last post by:
Hi: I'm developing a web application that needs to have five values, each retrieved from cookies on many pages. If I have five "Request.Cookies" commands together does this cause five "round...
2
by: Fred Nelson | last post by:
Hi: I'm working on a VS2005 web application and I have what is probabably a "newby" question. In VS2003 I could drag a textbox/button/etc on to a form and position it with the mouse. I...
2
by: johnnyG | last post by:
Greetings, I'm studying for the 70-330 Exam using the MS Press book by Tony Northrup and there are 2 side-by-side examples of using the SHA1CryptoServiceProvider to create a hash value from a...
10
by: Charles Russell | last post by:
Why does this work from the python prompt, but fail from a script? How does one make it work from a script? #! /usr/bin/python import glob # following line works from python prompt; why not in...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.