Newby C++ vs. C question

Discussion in 'C++' started by alexrixhardson@yahoo.com, Jul 31, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jul 31, 2007
    #2
    1. Advertising

  3. Default User Guest

    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
    Default User, Jul 31, 2007
    #3
  4. James Kanze Guest

    On Jul 31, 4:49 pm, 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:
    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
    James Kanze, Jul 31, 2007
    #4
  5. Daniel T. Guest

    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.
    Daniel T., Jul 31, 2007
    #5
  6. Guest

    Thank you all for your comments. You made things more clear to me now.
    , Aug 1, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hermann W Ehlers

    Newby question

    Hermann W Ehlers, May 2, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    685
    Matt Berther
    May 5, 2004
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    ASP.NET easy newby question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    452
    Kevin Spencer
    Nov 23, 2004
  3. John Baker
    Replies:
    3
    Views:
    294
    Kevin Spencer
    Feb 8, 2005
  4. John Baker

    Rephrase of Newby Question!

    John Baker, Feb 8, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    551
    Michael C#
    Feb 9, 2005
  5. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    626
    Iain Norman
    Feb 16, 2005
Loading...

Share This Page