Generally, are the programs written by C++ slower than written by C10% ?

Discussion in 'C++' started by KaiWen, Sep 1, 2011.

  1. KaiWen

    KaiWen Guest

    Generally, are the programs written by C++ slower than written by C
    10% ?
    KaiWen, Sep 1, 2011
    #1
    1. Advertising

  2. KaiWen

    Ian Collins Guest

    Re: Generally, are the programs written by C++ slower than writtenby C 10% ?

    On 09/ 1/11 08:41 PM, KaiWen wrote:
    > Generally, are the programs written by C++ slower than written by C
    > 10% ?


    No, assuming you intended to write in rather than by...

    --
    Ian Collins
    Ian Collins, Sep 1, 2011
    #2
    1. Advertising

  3. Re: Generally, are the programs written by C++ slower than written by C 10% ?

    "KaiWen" wrote in message
    news:...

    > Generally, are the programs written by C++ slower than written by C 10% ?


    It is not clear to me what you mean.
    C++ is more complicated than C, so C++ programs are written somewhat slower
    that C programs, because more thinking is needed. But, at the other hand,
    C++ usually needs less source code to complete a complex program (because of
    the availability of a rich library and the reuse of code in templates), so
    writing a program may be completed faster in C++ that in C.
    Fred Zwarts \(KVI\), Sep 1, 2011
    #3
  4. KaiWen

    A Guest

    Re: Generally, are the programs written by C++ slower than written by C 10% ?

    It is not actually the language that might be slower because you can do
    equal things in C and C++ that execute approximately the same speed, what
    matters might be overhead that some C++ constructs imply on the program. But
    that is only if you use such constructs... such as templates for example.
    Even templates can also be used in a right and wrong way. So the answer is
    far from being general. In good hands each tool can work wonders or be just
    as average as any other tool.

    For example:

    std::vector<int> abc;
    abc.at(0) = 1; // is slower than
    abc[0] = 1; // than this because of range checking

    In C you don't even have such constructs so there is nothing to compare it
    to. But if you use at() which does some additional overhead due to range
    checking then of course it is somewhat slower than the other version. It is
    like you would call more functions in C, that would execute slower due to
    heap/stack allocation etc.

    But in C++ you could as well write:

    char[] abc = "abc";
    abc[0] = "x";

    that would work equally fast in C or C++.

    In general, there is only one REAL issue - which of the two you find more
    valuable - the convenience that C++ offers over C to speedup development...
    or... your time.... to finish a certain project (even if it is a bit
    slower). Remember, programmers time is never getting more expensive it may
    only get cheaper.
    A, Sep 1, 2011
    #4
  5. KaiWen

    Rui Maciel Guest

    Rui Maciel, Sep 1, 2011
    #5
  6. Re: Generally, are the programs written by C++ slower than written byC 10% ?

    On Sep 1, 2:48 am, "Fred Zwarts \(KVI\)" <> wrote:
    > "KaiWen"  wrote in message
    >
    > news:...
    >
    > > Generally, are the programs written by C++ slower than written by C 10%?

    >
    > It is not clear to me what you mean.
    > C++ is more complicated than C, so C++ programs are written somewhat slower
    > that C programs, because more thinking is needed. But, at the other hand,
    > C++ usually needs less source code to complete a complex program (becauseof
    > the availability of a rich library and the reuse of code in templates), so
    > writing a program may be completed faster in C++ that in C.


    Do you have any reliable evidence to make such a claim? I believe this
    is a complete pull out of thin air without any supporting evidence.
    I'll add my own pull out of thin air and claim that this is a non-
    obvious claim.

    I'll go even further actually. As long as the C++ constructs can map
    nicely to "equivalent" C constructs, I see no reason why C++ programs
    will be slower with a good compiler. It depends heavily on the exact
    programs. Usually people make unfair apples-to-oranges comparisons of
    an OO approach vs a procedural approach. When doing legitimate
    comparisons, you will find the C and C++ program are much closer than
    you think.
    Joshua Maurice, Sep 1, 2011
    #6
  7. KaiWen

    KaiWen Guest

    Re: Generally, are the programs written by C++ slower than written byC 10% ?

    On 9ÔÂ2ÈÕ, ÉÏÎç6ʱ34·Ö, Joshua Maurice <> wrote:
    > On Sep 1, 2:48 am, "Fred Zwarts \(KVI\)" <> wrote:
    >
    > > "KaiWen" wrote in message

    >
    > >news:....

    >
    > > > Generally, are the programs written by C++ slower than written by C 10% ?

    >
    > > It is not clear to me what you mean.
    > > C++ is more complicated than C, so C++ programs are written somewhat slower
    > > that C programs, because more thinking is needed. But, at the other hand,
    > > C++ usually needs less source code to complete a complex program (because of
    > > the availability of a rich library and the reuse of code in templates),so
    > > writing a program may be completed faster in C++ that in C.

    >
    > Do you have any reliable evidence to make such a claim? I believe this
    > is a complete pull out of thin air without any supporting evidence.
    > I'll add my own pull out of thin air and claim that this is a non-
    > obvious claim.
    >
    > I'll go even further actually. As long as the C++ constructs can map
    > nicely to "equivalent" C constructs, I see no reason why C++ programs
    > will be slower with a good compiler. It depends heavily on the exact
    > programs. Usually people make unfair apples-to-oranges comparisons of
    > an OO approach vs a procedural approach. When doing legitimate
    > comparisons, you will find the C and C++ program are much closer than
    > you think.


    We don't discuss OO here, just discuss the cost of compile time and
    the speed of program.
    1. Compile a C++ program is slower than written by C
    2. In runtime, the speed of the program written by C++ is <= the same
    program written by C

    I want to write a program about network, parse huge number of data,
    so, with all due respect, I can not decide witch language to use.

    I know I can develop faster by using C++, but the speed of program
    in runtime is important too. So witch one is important? Can anyone
    give a suggestion?
    KaiWen, Sep 2, 2011
    #7
  8. KaiWen

    Ian Collins Guest

    Re: Generally, are the programs written by C++ slower than writtenby C 10% ?

    On 09/ 2/11 02:09 PM, KaiWen wrote:
    >
    > I want to write a program about network, parse huge number of data,
    > so, with all due respect, I can not decide witch language to use.
    >
    > I know I can develop faster by using C++, but the speed of program
    > in runtime is important too. So witch one is important? Can anyone
    > give a suggestion?


    A poor choice of algorithm will run just as slow in well written C as in
    well written C++.

    A good choice of algorithm will run just as fast in well written C++
    (and maybe faster if generic code such as sort is used) as in well
    written C.

    --
    Ian Collins
    Ian Collins, Sep 2, 2011
    #8
  9. KaiWen

    Krice Guest

    Re: Generally, are the programs written by C++ slower than written byC 10% ?

    On 2 syys, 05:09, KaiWen <> wrote:
    > but the speed of program in runtime is important too.


    The speed depends on programming skills more than small
    differences between C and C++.
    Krice, Sep 2, 2011
    #9
  10. KaiWen

    Goran Guest

    Re: Generally, are the programs written by C++ slower than written byC 10% ?

    On Sep 2, 4:09 am, KaiWen <> wrote:
    > On 9ÔÂ2ÈÕ, ÉÏÎç6ʱ34·Ö, Joshua Maurice <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Sep 1, 2:48 am, "Fred Zwarts \(KVI\)" <> wrote:

    >
    > > > "KaiWen" wrote in message

    >
    > > >news:....

    >
    > > > > Generally, are the programs written by C++ slower than written by C10% ?

    >
    > > > It is not clear to me what you mean.
    > > > C++ is more complicated than C, so C++ programs are written somewhat slower
    > > > that C programs, because more thinking is needed. But, at the other hand,
    > > > C++ usually needs less source code to complete a complex program (because of
    > > > the availability of a rich library and the reuse of code in templates), so
    > > > writing a program may be completed faster in C++ that in C.

    >
    > > Do you have any reliable evidence to make such a claim? I believe this
    > > is a complete pull out of thin air without any supporting evidence.
    > > I'll add my own pull out of thin air and claim that this is a non-
    > > obvious claim.

    >
    > > I'll go even further actually. As long as the C++ constructs can map
    > > nicely to "equivalent" C constructs, I see no reason why C++ programs
    > > will be slower with a good compiler. It depends heavily on the exact
    > > programs. Usually people make unfair apples-to-oranges comparisons of
    > > an OO approach vs a procedural approach. When doing legitimate
    > > comparisons, you will find the C and C++ program are much closer than
    > > you think.

    >
    > We don't discuss OO here, just discuss the cost of compile time and
    > the speed of program.
    > 1. Compile a C++ program is slower than written by C
    > 2. In runtime, the speed of the program written by C++ is <= the same
    > program written by C


    For all practical intents and purposes, C++ is a subset of C.
    Therefore, both these claims are theoretically false. ;-)

    +1 for those who said that speed (and compile time, really) will
    depend much, much more on your team's skills with C or C++ than on
    some "inherent" difference between the two.

    In other words, you're going about it in a wrong way.

    > I want to write a program about network, parse huge number of data,
    > so, with all due respect, I can not decide witch language to use.


    Even worse. If that is so, then know this: network is orders of
    magnitude "slower" than either C or C++. The time needed to establish
    connections and transfer data between peers will easily swamp time
    spent in your code.

    >
    > I know I can develop faster by using C++, but the speed of program
    > in runtime is important too. So witch one is important? Can anyone
    > give a suggestion?


    Best suggestion I can give from what you have said so far is: speed-
    wise, it is, by and large, IRRELEVANT whether you pick C or C++ (And,
    you may just as well go for, I dunno, Java and still not have a
    relevant difference in speed.)

    Goran.
    Goran, Sep 2, 2011
    #10
  11. KaiWen

    Rui Maciel Guest

    Re: Generally, are the programs written by C++ slower than written by C 10% ?

    KaiWen wrote:

    > We don't discuss OO here, just discuss the cost of compile time and
    > the speed of program.
    > 1. Compile a C++ program is slower than written by C


    How is this relevant?


    > 2. In runtime, the speed of the program written by C++ is <= the same
    > program written by C


    This, as I already pointed out, is false.


    > I want to write a program about network, parse huge number of data,
    > so, with all due respect, I can not decide witch language to use.


    Possibly you are better served by using the language you are most proficient
    with. Compilation time is practically irrelevant and speed efficiency
    doesn't depend on which of these two languages you use, only on what code
    you write.


    Rui Maciel
    Rui Maciel, Sep 2, 2011
    #11
  12. Re: Generally, are the programs written by C++ slower than written byC 10% ?

    On Sep 2, 7:23 am, Goran <> wrote:
    > On Sep 2, 4:09 am, KaiWen <> wrote:
    > > On 9ÔÂ2ÈÕ, ÉÏÎç6ʱ34·Ö, Joshua Maurice <> wrote:
    > > > On Sep 1, 2:48 am, "Fred Zwarts \(KVI\)" <> wrote:
    > > > > "KaiWen" wrote in message
    > > > >news:...


    > > > > > Generally, are the programs written by C++ slower than written byC 10% ?

    >
    > > > > It is not clear to me what you mean.
    > > > > C++ is more complicated than C, so C++ programs are written somewhat slower
    > > > > that C programs, because more thinking is needed. But, at the otherhand,
    > > > > C++ usually needs less source code to complete a complex program (because of
    > > > > the availability of a rich library and the reuse of code in templates), so
    > > > > writing a program may be completed faster in C++ that in C.

    >
    > > > Do you have any reliable evidence to make such a claim? I believe this
    > > > is a complete pull out of thin air without any supporting evidence.
    > > > I'll add my own pull out of thin air and claim that this is a non-
    > > > obvious claim.

    >
    > > > I'll go even further actually. As long as the C++ constructs can map
    > > > nicely to "equivalent" C constructs, I see no reason why C++ programs
    > > > will be slower with a good compiler. It depends heavily on the exact
    > > > programs. Usually people make unfair apples-to-oranges comparisons of
    > > > an OO approach vs a procedural approach. When doing legitimate
    > > > comparisons, you will find the C and C++ program are much closer than
    > > > you think.

    >
    > > We don't discuss OO here, just discuss the cost of compile time and
    > > the speed of program.
    > > 1. Compile a C++ program is slower than written by C
    > > 2. In runtime, the speed of the program written by C++ is <= the same
    > > program written by C

    >
    > For all practical intents and purposes, C++ is a subset of C.


    seriously?

    > Therefore, both these claims are theoretically false. ;-)


    even if C were a subset of C++ I don't see how the statment is
    theoretically untrue

    > +1 for those who said that speed (and compile time, really) will
    > depend much, much more on your team's skills with C or C++ than on
    > some "inherent" difference between the two.
    >
    > In other words, you're going about it in a wrong way.
    >
    > > I want to write a program about network, parse huge number of data,
    > > so, with all due respect, I can not decide witch language to use.

    >
    > Even worse. If that is so, then know this: network is orders of
    > magnitude "slower" than either C or C++. The time needed to establish
    > connections and transfer data between peers will easily swamp time
    > spent in your code.
    >
    >
    >
    > > I know I can develop faster by using C++, but the speed of program
    > > in runtime is important too. So witch one is important? Can anyone
    > > give a suggestion?

    >
    > Best suggestion I can give from what you have said so far is: speed-
    > wise, it is, by and large, IRRELEVANT whether you pick C or C++ (And,
    > you may just as well go for, I dunno, Java and still not have a
    > relevant difference in speed.)
    >
    > Goran.- Hide quoted text -
    >
    > - Show quoted text -
    Nick Keighley, Sep 2, 2011
    #12
  13. KaiWen

    Rui Maciel Guest

    Re: Generally, are the programs written by C++ slower than written by C 10% ?

    Goran wrote:

    > For all practical intents and purposes, C++ is a subset of C.


    I don't believe that features such as support for object-oriented
    programming, polymorphism, templates and namespaces can be excluded from
    "practical intents".


    Rui Maciel
    Rui Maciel, Sep 2, 2011
    #13
  14. KaiWen

    Goran Guest

    Re: Generally, are the programs written by C++ slower than written byC 10% ?

    On Sep 2, 12:00 pm, Nick Keighley <>
    wrote:
    > On Sep 2, 7:23 am, Goran <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Sep 2, 4:09 am, KaiWen <> wrote:
    > > > On 9ÔÂ2ÈÕ, ÉÏÎç6ʱ34·Ö, Joshua Maurice <> wrote:
    > > > > On Sep 1, 2:48 am, "Fred Zwarts \(KVI\)" <> wrote:
    > > > > > "KaiWen" wrote in message
    > > > > >news:..com...
    > > > > > > Generally, are the programs written by C++ slower than written by C 10% ?

    >
    > > > > > It is not clear to me what you mean.
    > > > > > C++ is more complicated than C, so C++ programs are written somewhat slower
    > > > > > that C programs, because more thinking is needed. But, at the other hand,
    > > > > > C++ usually needs less source code to complete a complex program (because of
    > > > > > the availability of a rich library and the reuse of code in templates), so
    > > > > > writing a program may be completed faster in C++ that in C.

    >
    > > > > Do you have any reliable evidence to make such a claim? I believe this
    > > > > is a complete pull out of thin air without any supporting evidence.
    > > > > I'll add my own pull out of thin air and claim that this is a non-
    > > > > obvious claim.

    >
    > > > > I'll go even further actually. As long as the C++ constructs can map
    > > > > nicely to "equivalent" C constructs, I see no reason why C++ programs
    > > > > will be slower with a good compiler. It depends heavily on the exact
    > > > > programs. Usually people make unfair apples-to-oranges comparisons of
    > > > > an OO approach vs a procedural approach. When doing legitimate
    > > > > comparisons, you will find the C and C++ program are much closer than
    > > > > you think.

    >
    > > > We don't discuss OO here, just discuss the cost of compile time and
    > > > the speed of program.
    > > > 1. Compile a C++ program is slower than written by C
    > > > 2. In runtime, the speed of the program written by C++ is <= the same
    > > > program written by C

    >
    > > For all practical intents and purposes, C++ is a subset of C.

    >
    > seriously?
    >
    > > Therefore, both these claims are theoretically false. ;-)

    >
    > even if C were a subset of C++ I don't see how the statment is
    > theoretically untrue


    Whoops, lapsus linguae there... (blushes). I meant superset, of
    course.

    Goran.
    Goran, Sep 2, 2011
    #14
  15. KaiWen

    BGB Guest

    Re: Generally, are the programs written by C++ slower than writtenby C 10% ?

    On 9/2/2011 1:59 AM, Rui Maciel wrote:
    > KaiWen wrote:
    >
    >> We don't discuss OO here, just discuss the cost of compile time and
    >> the speed of program.
    >> 1. Compile a C++ program is slower than written by C

    >
    > How is this relevant?
    >


    depends on the tasks.

    although not as likely to be an issue for statically compiled C vs C++
    (except maybe for someone worried about build times, or people getting
    bored and having a coffee break because building takes so long, ...) it
    may not matter much.

    in terms of, say, writing a compiler for a script language which is
    often loaded-from-source (slower compile times thus meaning the app
    takes longer to start up, ...), then having a fast compiler matters a
    lot more.


    >
    >> 2. In runtime, the speed of the program written by C++ is<= the same
    >> program written by C

    >
    > This, as I already pointed out, is false.
    >


    actually, I think it can go either way (depending on programming styles
    in each case, ...).

    however, if in both cases it is the same code (common subset of C and
    C++), with no "evil tricks" in the background (overloaded operators,
    ....), one wouldn't expect to find much real difference.


    >
    >> I want to write a program about network, parse huge number of data,
    >> so, with all due respect, I can not decide witch language to use.

    >
    > Possibly you are better served by using the language you are most proficient
    > with. Compilation time is practically irrelevant and speed efficiency
    > doesn't depend on which of these two languages you use, only on what code
    > you write.
    >


    yep.
    BGB, Sep 2, 2011
    #15
  16. Re: Generally, are the programs written by C++ slower than written by C 10% ?

    KaiWen <> wrote:
    > 2. In runtime, the speed of the program written by C++ is <= the same
    > program written by C


    Actually many equivalent constructs in C and C++ are such that it's
    the exact opposite: C++ is significantly faster than C.

    Just two simple examples are qsort() vs. std::sort() , and strlen()
    vs std::string::length(). Especially in the first case it's precisely
    the language features that help the compiler make a faster executable.
    Juha Nieminen, Sep 3, 2011
    #16
  17. KaiWen

    Nobody Guest

    Re: Generally, are the programs written by C++ slower than written by C 10% ?

    On Thu, 01 Sep 2011 19:09:01 -0700, KaiWen wrote:

    > I know I can develop faster by using C++, but the speed of program
    > in runtime is important too. So witch one is important? Can anyone
    > give a suggestion?


    C code is usually either valid C++ code or is trivially modifiable to be
    valid C++ code. Whether such code runs faster when compiled as C or as C++
    depends upon the quality of the compiler, not the language.

    However: C++ has additional features over C. Whether those features make
    the program faster or slower depends upon how you use them.

    No language feature is inherently "slow". Even if a particular feature has
    a performance cost, if it simplifies development then the time saved can
    be spent optimising other parts of the code.

    The question isn't which language you would use if you wanted absolutely
    optimal code (otherwise, everyone would use assembler). The question is
    which language will produce the best result given the development time
    available.

    The answer to that question depends more upon the skill of the programmers
    than on the language.

    On one hand, C++ has features which will allow a good programmer to both
    improve performance and reduce development time simultaneously. On the
    other hand, not all programmers are good programmers, and C++ provides
    not-so-good programmers with far greater scope for messing up.
    Nobody, Sep 3, 2011
    #17
  18. KaiWen

    BGB Guest

    Re: Generally, are the programs written by C++ slower than writtenby C 10% ?

    On 9/2/2011 10:51 PM, Juha Nieminen wrote:
    > KaiWen<> wrote:
    >> 2. In runtime, the speed of the program written by C++ is<= the same
    >> program written by C

    >
    > Actually many equivalent constructs in C and C++ are such that it's
    > the exact opposite: C++ is significantly faster than C.
    >
    > Just two simple examples are qsort() vs. std::sort() , and strlen()
    > vs std::string::length(). Especially in the first case it's precisely
    > the language features that help the compiler make a faster executable.


    this again, comes down to coding practices...


    also, just how many people really use "qsort()" anyways?...
    IME, it is more often one of those largely forgotten functions (it is
    there, but more often people write out their own sort logic manually).

    (actually, it seems to be fairly common practice in C land for people to
    largely ignore much of the standard library, and to write their own
    logic for doing things).


    it is also not clear that std::string would be, in general, faster, and
    having an O(1) ability to fetch the length may turn out to be moot if
    most other operations tend towards being more expensive. not that
    worrying about micro-optimizing string operations is usually all that
    relevant though anyways.


    from what I can gather from checking online (via Google), it seems where
    people have tried testing, the performance differences have tended to be
    negligible.

    some other tests have apparently shown std::string operations to be
    about 4x slower than C-style strings.
    BGB, Sep 3, 2011
    #18
  19. KaiWen

    Ian Collins Guest

    Re: Generally, are the programs written by C++ slower than writtenby C 10% ?

    On 09/ 3/11 08:45 PM, BGB wrote:
    > On 9/2/2011 10:51 PM, Juha Nieminen wrote:
    >> KaiWen<> wrote:
    >>> 2. In runtime, the speed of the program written by C++ is<= the same
    >>> program written by C

    >>
    >> Actually many equivalent constructs in C and C++ are such that it's
    >> the exact opposite: C++ is significantly faster than C.
    >>
    >> Just two simple examples are qsort() vs. std::sort() , and strlen()
    >> vs std::string::length(). Especially in the first case it's precisely
    >> the language features that help the compiler make a faster executable.

    >
    > this again, comes down to coding practices...
    >
    >
    > also, just how many people really use "qsort()" anyways?...
    > IME, it is more often one of those largely forgotten functions (it is
    > there, but more often people write out their own sort logic manually).
    >
    > (actually, it seems to be fairly common practice in C land for people to
    > largely ignore much of the standard library, and to write their own
    > logic for doing things).


    That says it all really, C++ programmers don't have to.

    --
    Ian Collins
    Ian Collins, Sep 3, 2011
    #19
  20. Re: Generally, are the programs written by C++ slower than written by C 10% ?

    BGB <> wrote:
    > also, just how many people really use "qsort()" anyways?...
    > IME, it is more often one of those largely forgotten functions (it is
    > there, but more often people write out their own sort logic manually).
    >
    > (actually, it seems to be fairly common practice in C land for people to
    > largely ignore much of the standard library, and to write their own
    > logic for doing things).


    This is exactly one of the reasons why I dislike the language so much.
    The C standard library does not offer many useful tools, and the reason
    for this is that the language doesn't lend itself for this. It's very
    hard to implement any generic data containers or algorithms which would
    be efficient, and because they will always have some compromises,
    inefficiencies or other defects, most C programmers end up not using
    them anyways.

    "Nobody uses qsort() anyways" is just the epitome of this. Contrast it
    with std::sort(), which is actually a really decent general use sorting
    function, and in the vast majority of cases it's pretty useful, and it's
    very commonly used in C++ in most situations where sorting is needed.
    Moreover, it's very easy to use (much easier than qsort()).

    > it is also not clear that std::string would be, in general, faster, and
    > having an O(1) ability to fetch the length may turn out to be moot if
    > most other operations tend towards being more expensive. not that
    > worrying about micro-optimizing string operations is usually all that
    > relevant though anyways.


    Optimizing from O(n) to O(1) is far from "micro-optimizing". (And it's
    not like you have to do anything in your code to achieve this.)
    Juha Nieminen, Sep 3, 2011
    #20
    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. Andre Charbonneau

    XPath queries getting slower and slower...

    Andre Charbonneau, Feb 15, 2005, in forum: Java
    Replies:
    0
    Views:
    537
    Andre Charbonneau
    Feb 15, 2005
  2. Marko Vrbanec

    Generally about HTML

    Marko Vrbanec, May 29, 2005, in forum: HTML
    Replies:
    1
    Views:
    396
    David Dorward
    May 29, 2005
  3. Richard Cavell
    Replies:
    7
    Views:
    1,855
    Andrey Tarasevich
    Mar 17, 2005
  4. T.A.
    Replies:
    21
    Views:
    866
    Pete Becker
    Sep 25, 2006
  5. Alf P. Steinbach /Usenet
    Replies:
    61
    Views:
    1,007
Loading...

Share This Page