Re: C++ more efficient than C?

Discussion in 'C Programming' started by Ben Bacarisse, Apr 6, 2008.

  1. "copx" <> writes:

    > In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that
    > properly written C++ outperforms C code. I will just copy his first example
    > here, which is supposed to demonstrate how C++ abstractions do not only make
    > code easier to understand but also make it more efficient:
    > ===
    > The simplest specific example I can think of is a program to find the mean
    > and median of a sequence of double precision floating-point numbers read
    > from input. A conventional C-style solution would be:


    <snip C>

    > To compare, here is an idiomatic C++ solution:


    <snip C++>

    > He goes on to claim that, using a sample set of 5,000,000 elements, the C++
    > code is more than 4 times faster than the C code. Further examples follow to
    > prove the superiority of C++ even in the realm of efficiency.


    I can't get anything like 4 times on my system. With optimisations
    off, the C code is a little faster, with optimisations on, the C code
    is 20% slower.

    All the speed is coming from the sort. Comment that out, and the C++
    is slower with or without optimisations. Not really surprising that,
    in an IO test, C would have the edge (without the sort, that is all
    this is).

    It is also not really surprising that C++'s sort can be optimised to
    be faster than qsort -- the language is designed to make such
    optimisations possible, but I wonder what combination of compiler and
    hardware gives a factor of 4. What do other people see?

    --
    Ben.
     
    Ben Bacarisse, Apr 6, 2008
    #1
    1. Advertising

  2. Ben Bacarisse

    Richard Guest

    "copx" <> writes:

    > "Ben Bacarisse" <> schrieb im Newsbeitrag
    > news:...
    > [snip]
    >> I can't get anything like 4 times on my system. With optimisations
    >> off, the C code is a little faster, with optimisations on, the C code
    >> is 20% slower.
    >>
    >> All the speed is coming from the sort. Comment that out, and the C++
    >> is slower with or without optimisations. Not really surprising that,
    >> in an IO test, C would have the edge (without the sort, that is all
    >> this is).
    >>
    >> It is also not really surprising that C++'s sort can be optimised to
    >> be faster than qsort -- the language is designed to make such
    >> optimisations possible, but I wonder what combination of compiler and
    >> hardware gives a factor of 4.

    >
    > The text contains any details on that. It only says:
    > "The implementation I used is widely available and cheap - not a research
    > toy. Implementations that claim higher performance are also available."
    > The text was published in the May 1999 issue of "The C/C++ User's Journal".
    > That's all available info - make your guess! ;)
    >
    > The factor of 4 seems to be a very case, however C++ does indeed seem to
    > beat C in synthetic benchmarks most of the time these days:
    > http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all
    > Funny side note: if you give equal importance to "CPU time", "Memory Use",
    > and "Gzip Bytes" the winner is... Pascal!


    Don't be ridiculous. This entire thread is clearly a troll.
     
    Richard, Apr 6, 2008
    #2
    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. maadhuu

    why is double more efficient than float ?

    maadhuu, Sep 14, 2005, in forum: C Programming
    Replies:
    14
    Views:
    850
    Emmanuel Delahaye
    Sep 17, 2005
  2. Replies:
    85
    Views:
    1,496
    James Kanze
    Apr 21, 2008
  3. Ian Collins

    Re: C++ more efficient than C?

    Ian Collins, Apr 6, 2008, in forum: C Programming
    Replies:
    1
    Views:
    396
    Ian Collins
    Apr 6, 2008
  4. Steven D'Aprano
    Replies:
    0
    Views:
    143
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    116
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page