473,800 Members | 2,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1830
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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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
2608
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 system.drawing.dll Additional information: Overflow or underflow in the arithmetic operation.
0
1840
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 stable release and once activated it requested access to the net. Without access it would come up with recognition and 'localhost' connection errors? Can anyone please tell me:
8
1835
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 downloaded Python; It's pretty good isn't it? It's particularly handy being able top run the same bytecode on different platforms.
10
2936
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 routines that upload and process images. I can't get past "square one" with images in C#: This statement:
4
357
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 only one case record that is delivered by a stored procedure. (I have a stored procedure that works so my question is only on loading the DataSet.) The DataSet will only be used for printing form letters and then will be zapped. I would like...
4
1281
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 trips" to the browser or am I accessing one instance. If five round trips occur then I will develop some parsing algorithm to save everything I need in one cookie.
2
4160
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 converted an app to VS2005 and this still worked. In VS2005 I can't do this. I have examined the code for VS2003 and I see that there is a style="Z-INDEX... " in the asp code for the
2
3711
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 string. The vb example outputs "A94A8FE5CCB19BA61C4C0873D391E987982FBBD3" The cs example outputs "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8" for the string "password" Question: do the 2 applications use different automatic "salts" or "seeds" to...
10
11526
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 script? files=glob.glob('*.py') print files Traceback (most recent call last):
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.