C, C++ or C#?

This is a discussion on C, C++ or C#? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Mario F. Not really. As long as it is part of the current standard defined for C++, ...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Mario F. View Post
    Not really. As long as it is part of the current standard defined for C++, it is C++. The language is clearly defined in the standard.
    If you only use the C part of C++, you are writing C code. Simple as that.
    Although even then, I think some extra stuff might get initialized when the program is loaded, such as support for exception handling... So even compiling C code with C++ could be microscopically slower than C with a C compiler.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by cpjust View Post
    If you only use the C part of C++, you are writing C code. Simple as that.
    Although even then, I think some extra stuff might get initialized when the program is loaded, such as support for exception handling... So even compiling C code with C++ could be microscopically slower than C with a C compiler.
    If C code is converted to C++, then one has access to C++'s much larger Standard Library, which has the potential to be faster than the corresponding C code, since it can be written in assembly. Any parts of C++ which would make the code slower can be avoided - except for the unavoidable things you mentioned, of course.

    Edit: Regarding exception handling, proper error checking in C, without exceptions, can be very tedious, but should be done anyway, since correctness is most important. The exception handling overhead is probably just equivalent to what would be needed in C to do the proper error checking.
    Last edited by robatino; 02-02-2008 at 01:57 PM.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by cpjust View Post
    If you only use the C part of C++, you are writing C code. Simple as that.
    Although even then, I think some extra stuff might get initialized when the program is loaded, such as support for exception handling... So even compiling C code with C++ could be microscopically slower than C with a C compiler.
    I doubt that. Perhaps it might start slower, but if you write C code, it's C code. It won't be slower nor faster.
    Of course, to be picky, there's a difference to the code when compiled with different compilers. The same C code could be slower when compiled in C++ due to the compiler being worse at optimizing than the C compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,458
    Quote Originally Posted by cpjust View Post
    If you only use the C part of C++, you are writing C code. Simple as that.
    No. You are not writing C code unless you intend to compile with a C compiler

    The language C++ is clearly defined.

    Code:
    int i = 12;
    Is the above C or C++ code? The answer is, both. It is C code and C++ code. It's not only C code. The compiler you use will interpret that its way. If you use a C++ compiler it will interpret that the C++ way. It will be C++ code.

    If, on the other hand you write,

    Code:
    extern "C" { int i = 12; }
    you are clearly defining C code this time.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, but that code is also valid in C#, Java and probably some others too.
    I think you're confusing what I'm saying. When the executable is compiled with C vs C++ it will almost certainly be different even though it's the same code.
    What I'm saying is when compiling a C++ program (i.e. a program that won't compile with anything other than a C++ compiler) then on average a program that does the same thing written in C will be faster than the C++ version. There are of course some examples where C++ would be faster, such as std::sort(), but in 99% of the real world programs I would say that C is faster.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    No way in hell.
    Write a program. Compile with both C and C++ and look at the assembly.
    I've looked at plenty C++ assembly, and believe me, it's only the "necessary" stuff to make everything go around. There's no "hidden" exceptions, unless you really do use them, and then things get slower.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    No way in hell.
    Write a program. Compile with both C and C++ and look at the assembly.
    I've looked at plenty C++ assembly, and believe me, it's only the "necessary" stuff to make everything go around. There's no "hidden" exceptions, unless you really do use them, and then things get slower.
    Wouldn't the real test be to time both programs when they run instead of just looking at the assembly? I'm not talking about simple Hello World programs either, I'm talking about large real world programs with large class hierarchies...

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    We've already established that using C++ elements has a small performance impact. It won't be as fast as pure C.
    But pure C compiled with C++ won't be slower than C compiled with a C compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,458
    Quote Originally Posted by cpjust View Post
    Wouldn't the real test be to time both programs when they run instead of just looking at the assembly?
    Most C++ compilers can also compile as C. You can test this yourself. However, looking at the assembly is less error prone.

    Quote Originally Posted by cpjust View Post
    I'm not talking about simple Hello World programs either, I'm talking about large real world programs with large class hierarchies...
    Class and hierarchies are not part of the C language.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #25
    Registered User
    Join Date
    Nov 2007
    Posts
    57
    Funny how the creator of the thread hasn't replied yet. :P

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Taking MichaelP's comment to the side for a moment, I would say that if you solve the same problem, using the same code, and compile with a C++ compiler and a C compiler of similar type [e.g MS VC++ in C++ and C settings, or g++ and gcc], then the execution time will be the same (or so darn close that you can't tell them apart). Reading the assembler code, assuming you are qualified, is a fine way to compare two compilers or different variants of the same code, etc.

    Even if C++ adds a bit of extra code to handle exceptions from time to time, it's generally not in the most "busy" code - if your really busy code is throwing exceptions [even rarely], then you probably should reconsider your design [I'm sure this will cause another bout of discussion as to whether throwing exceptions is better or worse than some other way of dealing with "bad data" or other circumanstances].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    But this is beside the point -- if the OP learns to program in C++ using only C functions and stuff, then they've just learned C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    ...But still has the ability to go "true" C++ later or mix the two into "C+". The best of both worlds, so to speak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,458
    Absolutely. Actually, as he would if he started with C++ and then started to grasp C code concepts as he went along his apprenticeship.

    What I do think though, is that speed differences is the less of anyone's concern. Definitely on what comes to these two languages alone.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21